-1

I am a php newbie and struggling to try and figure this one out. I have page where I am returning all the newsroom posts. The posts have a newsroom type that further divides them into Awards, Press Releases, In the News. I want to display the posts in groups so that all of the Press Releases show up first, then the awards and finally In the News. Within each group I want to further sort by date (newest first). Currently, I have this in my functions.php file to set up the query.

add_action( 'elementor/query/newsroom_order', function( $query ) {
    $query->set( 'post_type', 'newsroom' );
    $query->set( 'posts_per_page', '12'); // unable to override number on actual page
});

I'm using FacetWP as well. I've tried to do an orderby but it doesn't allow me to set the actual order.

These are the values:

  • Taxonomy = newsroom_type
  • post_type = newsroom
  • slug OR term_id for:
    • awards, 1822
    • press_releases, 1812
    • in_the_news, 1814

I'm hoping someone can tell me a good way to accomplish this.

2
  • I'm not sure what the difference is between type and term ID, since the term ID is the ID of a type, and all posts have the post type newsroom, based on your description 2 of the items you want to sort by are the same thing so there are 2 not 3 sorting criteria. Note that sorting by term is rare and not normal since a post can appear in more than one term making sorting very complicated if not impossible, normally people use multiple queries to solve this rather than a single query
    – Tom J Nowell
    Mar 25 at 19:34
  • Note that Elementor dev support questions are offtopic here and not allowed on this stacks, any answer you get here will only be in the context of generic WordPress solutions. Elementor specifics are offtopic and you should ask at an Elementor community or Elementor support.
    – Tom J Nowell
    Mar 28 at 13:36

1 Answer 1

0

You can use the orderby parameter and pass it an array of what you want to order by in the order you want it ordered by:

https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

This solves the problem of ordering by post type, and by date, but ordering by term slug or term ID is not something WP_Query can do. It may make sense to you as your posts only appear in a single newsroom_type but nothing prevents an item being both an award and a press release at the same time in the database, so sorting by terms doesn't make sense, especially since terms have no inherent order that can be used.

The closest is alphabetic, but that complicates sorting further, e.g. if a post has term A and G, does it come after or before a post with the term B? What if B is a child term of G? Sorting by terms is a massive can of worms that has never been tackled or solved. You can't sort by terms with WP_Query. You can group/search/filter for posts by terms though.

The best way to do this is to instead do 3 separate queries.

2
  • So yes, all 3 have the same post type but have different slugs and term ids. I tried the orderby but I was unable to get it working. How would I get that to work in this case? I think it "should" do what I need but the syntax was alluding me.
    – elms
    Mar 28 at 13:00
  • it is not possible to do this by changing the parameters of and options/arguments. A separate independent query is required for each term you want to include so that you can manually order them. E.g. do a query for A, then do a query for B, then do a query for C, and display the results of each query in that order. Since you are using an Elementor widget that means 3 Elementor widgets, you would need to ask in an Elementor community or via Elementor support if you want to improve on that though, as 3rd party plugin dev support questions are offtopic here
    – Tom J Nowell
    Mar 28 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.