Orderby custom field alters the Loop in wordpress

Asked

Viewed 66 times

0

In my blog, I have different categories, and in each post I created a custom field with the key posicao with numeric values. This is the code I have

    $query = new WP_Query(array(
        'meta_key'          => 'posicao',
        'orderby'           => 'meta_value_num',
        'order'             => 'ASC'
    ));

    if ( $query->have_posts() ) : 

            while ( $query->have_posts() ) : $query->the_post();

                the_content();>     

            endwhile; 

            wp_reset_postdata();

            else: echo "Não existem artigos";

            endif;

However, on the category page, it no longer organizes by category, but rather, by the order of the key position , but shows all posts of all categories.

Thus, on the page of a category, it shows not only the posts of the category, but if I take the variables, as in the example below, it shows only the posts of the categories, but without the order by posicao

    if (have_posts() ) : 

            while (have_posts() ) : the_post();

1 answer

0


I managed to accomplish with

if (is_category( )) {
  $cat = get_query_var('cat');
  $yourcat = get_category ($cat);
  $yourcats =  $yourcat->slug;

 }

query_posts('category_name='. $yourcats.'&meta_key=posicao&orderby=meta_value_num&order=ASC&posts_per_page=-1'); 
if ( have_posts() ) : while ( have_posts() ) : the_post(); 

    the_content();

endwhile; else: endif; 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.