Changing Parameters of Default Query in Oxygen

With both Easy Posts & Repeater elements in Oxygen, it’s best to choose the default query where possible for archives. This allows Oxygen to just let WordPress take over & use the main query.

Choosing default query is what ensures that category pages will always show the posts from the correct category, search pages will correctly show the search results & the posts page will always show the latest posts etc.

You’ll notice, by relying on the main query from WordPress, we no longer have any controls for changing parameters such as ordering, filtering or count.

This is expected.

But let’s say we wanted to change the order of posts on our search results page, or filter the type of results that are shown. We still need to use the default query, to ensure the search results display properly, but we also need to control the parameters of that query.

The way to customise the main query in WordPress is to use pre_get_posts. That link is to the official WP documentation where it shows you how to use it, we’ll take out a few examples..

Change Order of Search Results

Here is an example of how to change the order of the posts in the search results. In Oxygen, this would be added to your code snippets.

add_action( 'pre_get_posts', 'search_filter' );
function search_filter($query) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if ( $query->is_search ) {
            $query->set( 'order', 'ASC' );
        }
    }
}

If you look at the code, you can see we are making sure we’re not in the admin page, then making sure the query is the main query. Lastly, we make sure the query is for the search results specifically, then finally we change the order to ASC (where DESC is the default).

Show only Posts in Search Results

Let’s say we only want to show posts in the search results, not pages or any custom post types. Then the code would be changed to this;

add_action( 'pre_get_posts', 'search_filter' );
function search_filter($query) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if ( $query->is_search ) {
            $query->set( 'post_type', 'post' );
        }
    }
}

Change Posts Per Page for Individual Archive Pages

Here we’re changing the posts per page for one particular archive (the recipes category).

add_action( 'pre_get_posts', 'lit_category_filter' );
function lit_category_filter($query) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if( $query->is_category('recipes')) {
            $query->set('posts_per_page', 1);
        }
    }
}

It’s worth reading through the examples of customising the main query using ‘pre_get_posts’ in the WP developer docs & experimenting to build your understanding.


Posted

in

by

Comments

6 responses to “Changing Parameters of Default Query in Oxygen”

  1. JoeDavid Avatar
    JoeDavid

    Can this be used in combination with https://wpdevdesign.com/other-cpt-entries-in-the-same-taxonomy-on-single-cpt-pages/ to add multiple related CPT repeaters on the same page?
    I think Yan’s code block does it, but I can’t get it to work twice on the same page. Any pointers?
    https://gist.github.com/yankiara/c43da83662a14e7609b4e23f6d82717a

    1. David Browne Avatar
      David Browne

      I’ll test this and see what I find. But I believe it should work, as long as you’re also adding the code block underneath each repeater.

      1. JoeDavid Avatar
        JoeDavid

        Thank you David, I really appreciate it. I was using the remove_action code after each repeater by the way.
        I’m trying to use it in a product template with two different taxonomies (Related by Categories and Related by Brand)

        1. David Browne Avatar
          David Browne

          May have to put this in new tutorial as there’s a few things to consider, but this code worked for me. https://gist.github.com/wplit/4d7c2814b7f002d20d18582e1ce2003d (top part in code block before repeater, bottom part in code block after repeater). It works doing this with multiple repeaters on a page, but make sure to change the function name for the second repeater (and obv the taxonomy name)

          1. JoeDavid Avatar
            JoeDavid

            Thank you David! I just tested and it works like a charm.

  2. gslweb Avatar
    gslweb

    Does this still work in 2022? Or is there a

    I have a static page with a repeater and when I add a code block with pre_get_posts, it doesn’t seem to affect the query in the repeater.

Leave a Reply to David Browne Cancel reply

Your email address will not be published. Required fields are marked *