0
votes

I create custom post type "portfolio" and create custom meta box with value key "_portfolio_name_value_key". but when I try to retrieve the saved value in frontend it did not show. i used this code: <?php get_post_meta( $post->ID, '_portfolio_name_value_key' ); ?> in single-portfolio.php page,

however title, featured image, custom taxonomy and content had no problem displaying. Here is full code I used:

<?php 
                    if ( have_posts() ) :
                    while ( have_posts() ) : the_post(); ?>


         <div class=row>
            <div class="col-xs-12 col-md-8 col-sm-8">
               <div class="owl-carousel owl-theme project-detail-carousel"id=project_detail>
                  <div class=project-detail-item><img alt=p-detail src="<?php the_post_thumbnail(''); ?>"></div>
                </div>
            </div>
            <div class="col-xs-12 col-sm-4 col-md-4">
               <div class=project-detail-1>
                  <h2><?php the_title(); ?></h2>
                  <ul>
                     <li><span>Client:</span><?php get_post_meta( $post->ID, '_portfolio_name_value_key' ); ?>
                     <li><span>Category:</span> <?php the_terms( $post->ID, 'field' ); ?>
                     <li><span>Status:</span> <?php get_post_meta( $post->ID, '_portfolio_status_value_key' ); ?>
                     <li><span>Tags:</span> <?php the_terms( $post->ID, 'ptag' ); ?>
                     <li><span>Date:</span> <?php get_post_meta( $post->ID, '_portfolio_date_value_key' ); ?>
                  </ul>
               </div>
            </div>
         </div>
         <div class="row mt-50">
            <div class=col-md-12>
               <div class=project-detail-1-info>
                  <h3>A little About This Project</h3>
                  <div class=text-content>
                    <?php the_content(); ?>
                  </div>
               </div>
            </div>
        </div>

        <?php 
        endwhile;
    endif;      

     ?>
3
thanks for respond but i used same code, it did not work, in my case i update code as <?php get_post_meta( $post_id, $key='_portfolio_name_value_key', $single=true ); ?>Sanzeev Dahal
You are in the loop so you need to put get_the_ID() function in place of $post->IDGufran Hasan
It did not work, however, title, thumbnail, content, taxonomy are working with $post->ID as in aboveSanzeev Dahal
Please go to post_meta table recheck custom field.Gufran Hasan

3 Answers

1
votes

As it seems you are using a loop. In the loop, you should use get_the_ID() function.

From:

<?php get_post_meta( $post->ID, '_portfolio_name_value_key' ); ?>

To:

<?php get_post_meta(get_the_ID(), '_portfolio_name_value_key' ); ?>
0
votes

Try This:-

<?php 

$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1));
                    if ( $wpb_all_query->have_posts() ) { ?>
	<div class=row>
            <div class="col-xs-12 col-md-8 col-sm-8">
               <div class="owl-carousel owl-theme project-detail-carousel"id=project_detail>
                  <div class=project-detail-item><img alt=p-detail src="<?php the_post_thumbnail(''); ?>"></div>
                </div>
            </div>
            <div class="col-xs-12 col-sm-4 col-md-4">
               <div class=project-detail-1>
                  <h2><?php the_title(); ?></h2>
                  <ul>
                     <li><span>Client:</span><?php get_post_meta( get_the_ID(), '_portfolio_name_value_key', true ); ?>
                     <li><span>Category:</span> <?php the_terms( get_the_ID(), 'field' ,true ); ?>
                     <li><span>Status:</span> <?php get_post_meta( get_the_ID(), '_portfolio_status_value_key' ,true ); ?>
                     <li><span>Tags:</span> <?php the_terms( get_the_ID(), 'ptag' ,true ); ?>
                     <li><span>Date:</span> <?php get_post_meta( get_the_ID(), '_portfolio_date_value_key' ,true ); ?>
                  </ul>
               </div>
            </div>
         </div>
         <div class="row mt-50">
            <div class=col-md-12>
               <div class=project-detail-1-info>
                  <h3>A little About This Project</h3>
                  <div class=text-content>
                    <?php the_content(); ?>
                  </div>
               </div>
            </div>
        </div>
		<?php wp_reset_postdata(); }?>
<ul>
 	<li><a href="#">Office Painting – the Easy Way</a></li>
 	<li><a href="#">12 Questions to Ask – <i>Before</i> You Hire a Painter</a></li>
 	<li><a href="http://route1paintingandwindows.com/price-service-sketchy-dudes/">Price, Service &amp; Sketchy Dudes</a></li>
 	<li><a href="http://route1paintingandwindows.com/top-3-must-haves-when-hiring-a-paint-contractor/">Top 3 Must-Haves When Hiring A Paint Contractor</a></li>
 	<li><a href="#">Top 5 Uninsured Contractor What-if’s</a></li>
</ul>
0
votes

get_post_meta() returns a value instead of echo it. So you need to echo it like this:

<?php echo get_post_meta( get_the_ID(), '_portfolio_date_value_key' ,true ); ?>