Sanzeev Dahal
Sanzeev Dahal

Reputation: 11

Get post meta data of custom post type

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;      

     ?>

Upvotes: 1

Views: 14210

Answers (3)

user15409935
user15409935

Reputation: 11

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 ); ?>

Upvotes: 1

Wakar Ahmad Khan
Wakar Ahmad Khan

Reputation: 507

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>

Upvotes: 0

Gufran Hasan
Gufran Hasan

Reputation: 9373

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' ); ?>

Upvotes: 1

Related Questions