Extend the where clause in WordPress WP_Query

Harish Kumar · · 1980 Views

In WordPress, sometimes needing to modify WP_Query class to interact with your database. For example, let's say you want to find all posts that the title would contain a specific keyword.

I guess your first attempt with WP_Query would be this:

$my_query = new WP_Query(
    array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 20,
        'orderby' => array('menu_order' => 'ASC', 'date' => 'DESC'),
        'update_post_meta_cache' => false,
        'update_post_term_cache' => false,
        's' => $search_key
    )
);

However, the above query searches the post content and title both. But we want it should search the post_title only.

I found out that WordPress offers a hook called posts_where. Using this hook, we can extend the where clause of the WP_Query. For that add the following lines in your functions.php file:

add_filter( 'posts_where', 'extend_wp_query_where', 10, 2 );
function extend_wp_query_where( $where, $wp_query ) {
    if ( $extend_where = $wp_query->get( 'extend_where' ) ) {
        $where .= " AND " . $extend_where;
    }
    return $where;
}

This way you can use the extend_where to customize your WP_Query.

$my_query = new WP_Query( array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 20,
    'orderby' => array('menu_order' => 'ASC', 'date' => 'DESC'),
    'update_post_meta_cache' => false,
    'update_post_term_cache' => false,
    'extend_where' => "(post_title like '%".$search_key."%' or post_title like '%".$search_key2."%')"
));
0

Please login or create new account to add your comment.

0 comments
You may also like:

How Can I Find Best WordPress Development Company In India 2023?

According to me there are various companies who are doing WordPress development but some of them are providing reliable word press development solutions.
Kenny William

Use Transients API Caching and Speed Up Your WordPress Theme.

The Transients API in WordPress is an effective method for saving cached data in the database. It allows us to take resource-intensive queries and store them in short-term caches (...)
Harish Kumar

Remove api.w.org REST API/JSON API from WordPress header.

WordPress uses the REST API since edition 4.4 of the CMS. It allows developers to interact with the WordPress back-end more quickly since this API is a standard way to connect. (...)
Harish Kumar

How to Add Custom User Profile (User meta) Fields In WordPress

When you are focusing on tasks that need user management, and you need to add more fields for the user. In that case, here user meta functionality is used. This is similar to creating (...)
Harish Kumar

WordPress: How to Fix Missing required field entry-title, Update, hCard Error in Google Structured Data tool.

Recently when I tested one of my WordPress weblogs via Google Structured Data testing tools, I got the following errors:
Harish Kumar

How to fetch Any post with WP_Query in WordPress?

WP_Query is your buddy. It allows you to get content from the database according to your requirements. In this article, I will explain top to bottom about how WP_Query works. let’s (...)
Harish Kumar