How to loop a custom post type by following the menu_order?

Asked

Viewed 2,993 times

1

On a static page in wordpress I’m looping a custom post type from a list of services as follows:

$temp = $wp_query;  
$wp_query = null;  
$wp_query = new WP_Query();  
$wp_query->query('showposts=14&post_type=servicos'.'&paged='.$paged);  

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

the_excerpt('');  

endwhile;  

$wp_query = null;  
$wp_query = $temp;  // Reset  

Beside I load a list of specialties through the code below:

wp_list_categories('taxonomy=Servicos&title_li=');  

Both I needed to sort following the order of the menu_order column in the wp_posts table. How could I do this?

1 answer

1


You need to take a look at Wordpress documentation, because on the reference page for Wp_query the parameters of order_by where you can use menu_order, in addition to the way you are doing this query using the global $wp_query it’s not the right thing. Another thing wrong is that showposts this obsolete since the version 2.1 of Wordpress which came out in January 2007...

Here the code of how it should be following the standards of Wordpress:

$new_query = new WP_Query( array(
    'posts_per_page' => 14,
    'post_type'      => servicos,
    'orderby'        => 'menu_order',
    'paged'          => $paged
) );

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

the_excerpt('');  

endwhile;  
wp_reset_postdata();

Already to wp_list_categories() there is no way to order by menu_order, see the parameters accepted by the function in the documentation and realize that menu_order is only for posts and not for the terms of your taxonomy, in case for taxonomies it is possible to use term_order, however to use term_order it is necessary to work with wp_get_object_terms() who accepts him as parameter.

  • Friend, that’s what I needed. Thanks for the very detailed explanation. Mine helped a lot. Thanks! Success!!

Browser other questions tagged

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