I can use WP_Query
to order posts matching my query by menu_order
.
But can I also specifically find posts that have a particular menu_order
?
Even better, can I find posts with a menu_order
larger than a particular number? Or smaller than a particular number?
If so, how?
Note: I can, of course, loop through posts that match my query and find the post I’m looking for in code, but it would be nicer if I can get the result directly from WP_Query
.
Matching by
menu_order
is easy, like this:However, larger or smaller option for
menu_order
is not provided by default inWP_Query
.You can still filter the where clause to achieve that. Something like this:
You can do the same thing for smaller check as well.
More advanced solution:
You can also enhance the
menu_order
argument so that the greater or lesser sign and the compare value is not hard coded within the filter function. Like this:With the above implementation, you can just change the
$args
to get the desired result. For example: