Sort posts by a custom menu order?
-
I have a front-page.php and use it to:
– Display all posts in a category (say “MyCatSomething”)
I use get_posts() and works perfectly 🙂– (this is what I cannot seem to make properly)
I want to order the posts by menu order.I made a custom menu, say “MyCustomMenu” (it is not displayed anywhere on the site, it’s only to rearrange the posts on the front page easily, with the mouse). I added all posts from MyCatSomething to that menu. But no matter how I rearrange the posts in that menu, the order does not change.
The code in front-page.php that makes or should make the “magic” work:
$args = array( 'posts_per_page' => 6, 'offset' => 0, 'category_name' => 'MyCatSomething', // 'sort_column' => 'menu_order', 'orderby' => 'menu_order', //'order' => 'ASC', 'post_type' => 'post', //'post_status' => 'publish', 'suppress_filters' => true ); $my_posts = get_posts( $args ); foreach ( $my_posts as $post ) : setup_postdata( $post ); ?> <?php get_template_part( 'template-parts/content-onfront', get_post_format() ); ?> <?php endforeach; wp_reset_postdata(); ?>I am making a custom theme, based on FoundationPress if that matters.
Searched a lot for similar solution, but didn’t find anything. 🙁
-
I have read a lot about this and came to the conclusion, that currently it is a feature not well supported by WordPress itself.
So, what can you do if you are in a similar situation like me? /to be able to easily sort posts in the back-end, that are displayed on the front page or any other page with a custom template file/
Option 1) Use /that is – manipulate/ the publishing date of the posts and sort by it
or
Option 2) Use the free plugin “Post Types Order” – there is a nice and easy to follow video tutorial on the plugin page. You need to install it and then you have a new submenu under Posts – “Re-Order” where you can order/reorder your posts as you wish.
Post Types Order pluginIf anybody knows of a different and direct approach /i.e. to order posts within the template files/, please add it as a comment. Thank you!
Try this:
$args = array( 'posts_per_page' => 6, 'category_name' => 'MyCatSomething', 'menu_order' => 'ASC', 'post_type' => 'post' );Check out this article on it
Lots more info on it here.
The topic ‘Sort posts by a custom menu order?’ is closed to new replies.