Example of WP_Query to search by post title in WordPress

Harish Kumar · · 16887 Views

One way to interact with the database is by using the global $wpdb object in WordPress. $wpdb is a WordPress database access abstraction class. This class used to interact with a database without needing to use raw SQL statements.

The recommended way to access $wpdb in your WordPress PHP code is to declare $wpdb as a global variable, like this:

<?php

// 1st Method - Declaring $wpdb as global and using it to 
// execute an SQL query statement that returns a PHP object
global $wpdb;

$query = "
        SELECT      *
        FROM        $wpdb->posts
        WHERE       $wpdb->posts.post_title LIKE '$param2%'
        ORDER BY    $wpdb->posts.post_title
";
$wpdb->get_results($query);

Alternate way and standard way to solve this operation is using filter:

<?php

add_filter( 'posts_where', 'qirolab_posts_where', 10, 2 );
function qirolab_posts_where( $where, &$wp_query )
{
    global $wpdb;
    if ( $title = $wp_query->get( 'search_title' ) ) {
        $where .= " AND " . $wpdb->posts . ".post_title LIKE '" . esc_sql( $wpdb->esc_like( $title ) ) . "%'";
    }
    return $where;
}

Now we can pass the title as the search_title argument in WP_Query.

<?php

$args = array(
        'post_type'        => 'post',
        'search_title'     => $param,
        'posts_per_page'   => $page_size,
        'paged'            => $page,
        'post_status'      => 'publish',
        'orderby'          => 'title', 
        'order'            => 'ASC',
);
 
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) : 
     while ( $wp_query->have_posts() ) : $wp_query->the_post();
            //Your Code to display post...
     endwhile;
endif;
0

Please login or create new account to add your comment.

0 comments
You may also like:

Data Integration Tools

An ocean of data integration tools that promise to be “the best” makes it easy to get confused. Based on research, usage experience, and popular ratings, we have compiled a (...)
Narola Infotech

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

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