• Hello All,

    I have posts in two categories that I want to list on a page,

    This is what I have:

    $args = array( 'post_type' => 'post', 'nopaging = true', 'category_name' => 'vacatures,jobs','nopaging = true');
    $the_query = new WP_Query($args);

    This works fine. Only thing is that the posts in category “jobs” get listed first, then “vacatures”

    I want this the other way around, so the “vacatures” category first.

    How could I do this?

    • This topic was modified 1 year, 1 month ago by eagerbob.
    • This topic was modified 1 year, 1 month ago by eagerbob.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    You’ve not specified any particular ordering in your query. The default ordering is by descending date. If jobs category posts occur first, I suspect it’s a mere coincidence of all jobs posts being published after the other category posts.

    There are many different ordering criteria possible with WP_Query, but sadly ordering by category term name is not one of them. To get ordering by term name you’d need to develop your own SQL query. You can replace the default WP_Query SQL with your own through the “posts_request” filter.

    Alternately, re-order the query results in $the_query->posts using PHP’s usort() prior to output. But sorting with PHP is not nearly as efficient as SQL sorting.

    Thread Starter eagerbob

    (@eagerbob)

    OK thanks. I figured that WP would run the first query first, then the next one. Before trying this I had two seperate queries and display them after each other. Works fine, but I want to filter the results with facet-wp. This requires/uses a query on the page. Not sure if facet-wp can search filter across categories, have to check that.

    Re-ordering the query results results is too complex. I look into the orderby options. Maybe I can make the “jobs” the child and “vacatures” the parent. See is I can order by parent (not sure how that would work)

    What I would prefer to do is to give them both the same category. They are both “jobs” or “vacancies” the difference being that posts in one category is managed from outside, the other is created from within WordPress. But since they have different single post templates they cannot be in the same category.

    I think I do some testing.

    Thanks again

    Moderator bcworkz

    (@bcworkz)

    Adding 'orderby' => 'parent', 'order' => 'ASC', to the query args might yield the results you are after. All parent pages will be listed first (parent_id = 0). Then all child pages, ordered by their parent’s ID. Assuming you implement a parent/child relationship of course.

    Is there really a parent/child relationship between pages, or is it an arbitrary relation solely to accomplish the ordering? If it’s arbitrary, why not be really arbitrary and order by menu_order? This is only available for pages, not posts. Actually, same goes for parent/child relations. In the page attributes meta box, the menu_order field is simply labeled as “Order”.

    If these listings are actually posts, neither option is available to you. However, the menu_order field is still part of a post object, but there’s no UI field available in posts to specify it. You could add a custom meta box to allow you to set menu_order for posts.

    If you’re going to be arbitrary, you could use a custom field to specify desired ordering. Then use 'orderby' => 'meta_value_num', arg.

    I’m unsure if any of these can be used by facet-wp, so compatibility with facet-wp ought to be the first thing you investigate.

    Thread Starter eagerbob

    (@eagerbob)

    Thanks again,

    I’ve got facet-wp working so that is good.

    Is there really a parent/child relationship between pages, or is it an arbitrary relation solely to accomplish the ordering? If it’s arbitrary, why not be really arbitrary and order by menu_order? This is only available for pages, not posts. 

    The “jobs” started out as pages indeed with a parent/child relation but I changed that to posts to match the “vacatures”, which are posts.

    The “vacatures” posts come from a CRM and add a whole list of custom taxonomies to the posts. They are not visible from the “edit post” page but they are visible from the “quick edit” page, strangely enough. So by adding these taxonomies terms to the “jobs” category posts facet-wp can work its magic across the two categories.

    I will try the parent-child varation for the taxonomies, see what that does. Will also make it easier for the visitor to drill down to what he is looking for.

    It is a bit of a puzzle to get it all working but we are getting there. Thanks for the tips.

    • This reply was modified 1 year, 1 month ago by eagerbob.
    Moderator bcworkz

    (@bcworkz)

    I will try the parent-child varation for the taxonomies

    You may do that, especially if it helps with the user experience in filtering into what they are looking for. However this will not be helpful in ordering the query results. Sorting by parent page is possible, but not by parent taxonomy term.

    What if you changed the “jobs” posts back to pages, or better yet a custom post type? Your WP_Query object could query for both types at the same time, ordering results primarily by “type”, secondarily by whatever makes sense, descending date perhaps?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘order of categories when querying multiple categories’ is closed to new replies.