• Resolved Franchi Web Design

    (@franchidesign)


    Hello,
    I can not get_post_meta to work inside a loop. Here is the code:

    <div class="container">
      <div class="row">
        <!-- Query Post Portfolio-->
        <?php
        $temp = $wp_query; $wp_query= null;
        $wp_query = new WP_Query(); $wp_query->query('posts_per_page=9' . '&paged='.$paged);
        while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <div class="col-lg-4 <?php
         // Retrieves the stored value from the database
         $meta_value = get_post_meta( get_the_ID() , 'meta-select', true );
         // Checks and displays the retrieved value
         if( !empty( $meta_value ) ) {
             echo $meta_value;
         }
        ?> col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic">
        <?php get_template_part( 'template-parts/content','post-portfolio-all-standar', get_post_type() );?>
        </div>
      <?php endwhile;
      wp_reset_postdata(); ?>
    </div>
    </div>
    

    where am I wrong? I checked the output, and the function always prints the first option, and the other options even if selected are not printed. Thank you so much

Viewing 2 replies - 16 through 17 (of 17 total)
  • Thread Starter Franchi Web Design

    (@franchidesign)

    Hello,
    finally it works, thanks also to your help.

    I worked all night and now it works perfectly.
    Here is the code:

    <div class="container">
        <div class="row">
          <!-- Query Post Portfolio-->
          <?php
          $portfolio = new WP_Query( array(
            'posts_per_page' => 6,
            'paged' => get_query_var( 'paged' )
          ) );
          
          if( $portfolio->have_posts() ) {
            while( $portfolio->have_posts() ) {
              $portfolio->the_post();
              global $wp_query;
              $meta_value = $wp_query->post->ID;
              ?>
              <div class="<?php
              if( !empty( $meta_value ) ) {
                 echo esc_attr(get_post_meta($meta_value, 'meta-select', true));
              }
              ?>  col-xs-12" data-aos="fade-up" data-aos-duration="1500" data-aos-easing="ease-out-cubic">
              <?php get_template_part( 'template-parts/content','post-portfolio-all-standar', get_post_type() );?>
            </div>
            
            <?php
          }
          wp_reset_postdata();
        }
        ?>
        
      </div>
    </div>
    

    Thanks again for the help and for my english !!!!
    ?? ??

    Moderator bcworkz

    (@bcworkz)

    Awesome! So it was an ID mismatch of a sort ?? Nice work!

Viewing 2 replies - 16 through 17 (of 17 total)
  • The topic ‘WordPress get_post_meta not work in loop’ is closed to new replies.