In this tutorial, we will see how can we reorder post within categories in WordPress. Sometime we may want to create series in the category, in that time it becomes essential to change post order in WordPress.
There is no need to search for WordPress post order plugin to do this. We can easily do it by using simple piece of code. I have break them in easy steps below :-
lets get started :-
What we are going to do ?
- Find the categories id of the post you want to order.
- Write the piece of code in
functions.php
to change the order of post in that category.
Step 1 : (Finding the ID of categories)
To find the id of the categories in the wordpress. Go to WordPress admin panel and go to Posts > Categories and find the name of categories that you want to find id.
Hover over to it and click on edit and take a notice of url in the browser for id. Your categories will be in the format wp-admin/term.php?taxonomy=category&tag_ID=6&post_type=post.
In above image is 6 is the categories id that you want to reorder. Take a note of it and open your functions.php by going to appearance > editor > functions.php
Step 2 : Add wordpress post order code in functions.php
Add these lines of code in your functions.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/* Reverse Post Order for Post Archives */ add_action( 'pre_get_posts', 'cih_custom_post_order' ); function cih_custom_post_order( $arg) { if ( is_admin() ) return; //Query to Change Archives if ( $arg->is_main_query() && is_archive() && !is_post_type_archive() && is_category() ) { $arg->set( 'orderby', 'date' ); // this orders by date $arg->set( 'order', 'ASC' ); // Changes the order to ascending order $arg->set( 'cat', '6' ); // Add your categories id here } } |
Thats it, its very easy. If you have any problem you can look the fix at WordPress codex and try to sort out yourself as well, else you can mention it via comments.
Leave a Reply