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.

Need help implementing a tutorial in your site or want to hire me for custom work?

HIRE ME

Find the article helpful and wish to donate?

DONATE

Comments

You need to be logged in to view and post comments.
Not a member yet? Join here.
magnifiercross
>
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
%d bloggers like this: