Extend the where clause in WordPress WP_Query

Harish Kumar · · 2213 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 to Build Flutter App for any WordPress Site

Leaving a perfect digital footprint is crucial for brand growth. However, you may be here to find the optimal balance between mobile engagement and web traffic. It’s possible (...)
Narola Infotech

The Benefits of Using Hospital Management Software in 2024: A Guide for Healthcare Providers

In the ever-evolving healthcare landscape, integrating technology has become imperative for efficient and effective patient care. One such technological advancement that has revolutionized (...)
anika

Types of Web Applications With Examples And Industry Use Cases

Whether it’s about driving more revenue for your business or strengthening your branding game, an impactful online presence is crucial. To make sure this is done right, there (...)
Narola Infotech

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