Sample code on how to apply the sort for Post Types Order plugin

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

The Post Types Order plugin is a great tool for sorting site content. It include the Autosort functionality, if turned on will auto-apply the customized order to all queries, there’s no need to modify any code. This makes it the ideal tool for people who don’t care about programming and just want an out-of-the-box tool.

Sort can also apply only to certain queries, the Autosort need to be turned OFF. The ‘orderby’ => ‘menu_order’ need to be included within custom query arguments.

$args = array(
              'post_type' => 'feature',
              'orderby'   => 'menu_order',
              'order'     => 'ASC'
            );
$my_query = new WP_Query($args);
while ($my_query->have_posts())
    {
        $my_query->the_post();
        (..your code..)          
    }

The sort can be reversed using the ‘order’ => ‘DESC

$args = array(
              'post_type' => 'feature',
              'orderby'   => 'menu_order',
              'order'     => 'DESC'
            );

When using Autosort, there is a filter pto/posts_orderby/ignore to ignore specific queries, more details at Ignore sort apply for certain query on Post Types Order

For advanced queries and sorts see the Advanced Post Types Order and it’s Documentation.


Subscribe
Notify of
guest
19 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dominik

Hello, can you show exactly where should I put this code?

nspcode

Hi,
Within your theme / custom code, you should first find the query you indent to apply the custom sorting capability. Then insert the ‘orderby’ => ‘menu_order’ .Or use the code above if you build a section from ground.

Thanks

Matt Hogeboom

Can you please answer the question? He asked you exactly where this code goes.

nspcode

Hi,
There is no specific place where to place a code within your theme, it’s just depends on it’s structure and requirements. However the majority prefer the functions.php located within the theme directory root.

Thanks

68mph

How would you edit the code to target only the home page feed and not all the recent posts widgets around my site? Thank you!

nspcode

i would check if the default_query is a feed e.g.
$wp_query->is_feed() see https://codex.wordpress.org/Conditional_Tags

John Hamman

Can you use this for the WordPress REST API?

Kedar

Perfectly done. Thankyou

Gilda De Araujo

Hi, The Post Re-order does not seem to be working on my blog. I’ve installed it. I have then reordered my posts through the drag and drop option, saved, and then refreshed my blog, but the posts have not changed positions? Please help. thanks http://www.bossladies.co.za

Chin Oh

I had the same issue just now but clearing my cache helped

nspcode

As Chin said, can you check if your blog use a cache plugin?

zuperuser

Hi,

the plugin is working excellent on my blog, but I’m experiencing a problem with the ‘next’ and ‘previous’ arrows below my posts.

I have this code to check if the post must hide or dispay the ‘next’ or the ‘previous’ arrow;

$cur_index = array_search( $post->ID, $portfolio_array );

if ( $cur_index > 0 ) {
$post_id = $portfolio_array[$cur_index-1];

echo ‘‘ . esc_html__( ‘Previous Project’, ‘alpha’ ) . ” . ” . get_the_title( $post_id ) . ‘‘;
}

if ( $cur_index + 1 < sizeof( $portfolio_array ) ) {
$post_id = $portfolio_array[$cur_index+1];

echo '‘ . esc_html__( ‘Next Project’, ‘alpha’ ) . ” . ” . get_the_title( $post_id ) . ‘‘;
}

How can I change the code so the arrows are displayed correctly? (and link to the correct post?

tia!

nspcode

Presuming the $portfolio_array variable contain the objects archive, they are not retrieved in the order you set-up. I suggest so you check with this link on how to retrieve a list of objects in the defined order https://www.nsp-code.com/sample-code-on-how-to-apply-the-sort-for-post-types-order-plugin/

As alternative, you can try the following:
1) Make sure the plugin setting “Next / Previous Apply” is active.
2) Use the previous / next_post_link() to output the links, through which the order is applied automatically
https://codex.wordpress.org/Function_Reference/previous_post_link
https://codex.wordpress.org/Template_Tags/next_post_link

Thanks

zuperuser

Hi,

Thank you for your reply!
The solution was embarrassingly simple: I have fixed the issue by setting the ‘Blog pages show at most’ in the WordPress Settings to more than 10… Duh…

cheers

nspcode

Or you can use ‘posts_per_page’ => -1 within your query arguments.

Thanks

SteveAx

Could someone help me.. the plug in is working and allowing me to drag and drop the posts.. my code to display them is:

query_posts(‘cat=1’);
while (have_posts()) : the_post();

then I display parts in divs.. anyway the sort seems to have no effect.. I tride changing the above to:

query_posts(‘cat=1&orderby=menu_order’);
while (have_posts()) : the_post();

with no effect.. any advice is appreciated.
Steve

nspcode

Try to include the ‘suppress_filters’ => FALSE within arguments, or better rely on full custom queries, see https://codex.wordpress.org/Class_Reference/WP_Query
Or make sure the plugin setting Auto Sort is turned on.

pravnb

Hi,
Im using this plugin, I just wish this plug in should have that simple option to reverse the sort order based on a post_date. But i didnt find it.

i just want to change Sort order based on Post_date, ASC order.
I’m not sure in which file i need to change and code for it.

Could you please help on it.

Thank you.

nspcode

Hi,
Sorry but the plugin apply the custom defined order. If you need to order by date, descending, you should just update your query arguments. Obliviously there is no specific file where to change, this is unique for each theme/plugin.

Thanks