Example of WP_Query to search by post title in WordPress

Harish Kumar · · 194 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:

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

How to Enable Featured Image in WordPress?

Featured Images or Post Thumbnails is a theme feature. Most themes such as Genesis and other themes support featured images by default. A great way to determine whether your theme (...)
Harish Kumar