4

How to sort the displayed posts by attribute "order"?

In functions.php I added the "Attributes" for posts

function wpzen_add_attributes_to_post() {
    add_post_type_support('post', 'page-attributes');
}
add_action('init', 'wpzen_add_attributes_to_post');
function wpzen_change_post_order($query) {
    if($query->is_main_query()) {
        $query->set('orderby', 'menu_order');
    }
}
add_action('pre_get_posts', 'wpzen_change_post_order');

The code displays an overview of posts looks like this:

<?php
        $show_posts = '12';
        $cat_name = 'Products';
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        $my_query = new WP_query('category_name=' . $cat_name . '&showposts=' . $show_posts . '&paged=' . $paged );

        global $wp_query;
        $wp_query->in_the_loop = true;

        while ($my_query->have_posts()) : $my_query->the_post();
        $do_not_duplicate = $post->ID;?>

    <a href="<?php echo get_permalink(); ?>">
        <h2><?php the_title(); ?></h2>
    </a>
    <?php the_content( $more_link_text , $strip_teaser ); ?>

<?php endwhile; ?>

How to sort posts according to the attribute "order" (ascending or descending).

1
  • Sorting posts by menu_order is a bad idea. Posts, unlike pages, are not hierarchical and their menu_order is normally 0 ( unless you change it manually or by a plugin ). Usually, you sort posts by something unique, like date ( default ) or ID. Sep 24 '17 at 3:45
2

Well, I'm afraid it won't work this way. Let me explain why...

In your wpzen_change_post_order function you check if $query->is_main_query(). To be precise, this checks, if given query is main query for current page - the query generated by WP to display default posts for current page.

So if you create your own WP_Query object, then this condition will be false and your function wont set orderby parameter.

How to change that?

There are to ways to fix that. First: remove the if statement from wpzen_change_post_order. But then you should be careful and add some other checks in there, so you'll be sure, that you modify only queries that you really want to. (Especially you should check, if ( ! is_admin() ))

Second way, much easier, I guess... Add orderby parameter directly in your WP_Query. So the code that displays posts would look like this:

<?php
        $show_posts = '12';
        $cat_name = 'Products';
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        $my_query = new WP_Query( array(  // <- It's better to use array
           'category_name' => $cat_name,
           'posts_per_page' => $show_posts,  // <- showposts is deprecated for long time, so use posts_per_page instead
           'paged' => $paged,
           'orderby' => 'menu_order'
        ) );

        global $wp_query;  // <- I'm not sure what do you want to accomplish with these 2 lines
        $wp_query->in_the_loop = true;

        while ($my_query->have_posts()) : $my_query->the_post();
        $do_not_duplicate = $post->ID;?>

    <a href="<?php echo get_permalink(); ?>">
        <h2><?php the_title(); ?></h2>
    </a>
    <?php the_content( $more_link_text , $strip_teaser ); ?>

<?php endwhile; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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