0

I need to do wordpress custom fields loop: here is the code to describe better:

<?php 
$term = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

$query = new WP_Query(array(
    'post_type' => 'recipe',
    'meta_key' => 'recipe_star_rating',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
   // 'taxonomy' => 'difficulty',
));



 global $wp_query;
       
      $total_results = $wp_query->found_posts;

      echo '<h2>"'.$term->name . '" Recipes</h2>';
      
      echo '<h4 class="tcount">'. $total_results . ' results</h4>';    ?>
      
     <hr class="aloha" />
           
    <?php order_properties_menu(); ?>
       
     <hr class="aloha" />
        
        
   <?php $counter = 0; ?>
   
<?php while ($query->have_posts()): $query->the_post();  ?>


    <div class="col-md-12 col-sm-6 col-xs-12">
                    <?php echo Chefscuisine_Template_Loader::load( 'recipes/grid' ); ?>
        </div>
        
     
            
        <?php $counter++; endwhile; ?>
        
          <? if ($counter % 4 === 0) { 
                echo '<div class="midad">Advert 336x280px Here!</div>';
            } ?>

I got listing but I need now by tahonomy eg. average-difficulty from taxonomy template, how to make it just by average difficulty or easy to cook thta are custom fields. Its pulling from category template called taxonomy-difficulties.php

1 Answer 1

0

I can just guess that there are numeric values inside recipe_star_rating post meta. Try this one if you have a numeric value, or just meta_value if there are string values.

$args = array(
    'order' => 'DESC',
    'meta_key' => 'recipe_star_rating',
    'orderby' => 'meta_value_num' 
); 

$query = new WP_Query( $args );

if ( $query->have_posts() ) :
    $counter = 0;
    while ( $query->have_posts() ) :  $query->the_post(); 
        $counter++;
        the_title(); 
        echo "<br>"; 
    endwhile; 
    wp_reset_postdata();
endif;
6
  • this is not working in my loop empty Commented Aug 12, 2021 at 21:04
  • Do you recieve any errors?
    – anton
    Commented Aug 12, 2021 at 21:11
  • I edit an answer. Noticed that you have the_post() inside your while loop, it should be $query->the_post(). Also you should reset postdata after custom WP_Query loop. Try this code, if it works, you can later modify it for your purpose (it works for "post" post type)
    – anton
    Commented Aug 12, 2021 at 21:19
  • anything does not work with this I got empty loop $args = array( 'orderby' => 'recipe_star_rating' , 'order' => 'DESC', ); Commented Aug 13, 2021 at 12:13
  • nothing works I have empty loop Commented Aug 13, 2021 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.