Sort post by attributes ‘order’

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="https://wordpress.stackexchange.com/questions/237185/<?php echo get_permalink(); ?>">
        <h2><?php the_title(); ?></h2>
    </a>
    <?php the_content( $more_link_text , $strip_teaser ); ?>

<?php endwhile; ?>

Leave a Comment