functions.php
<?php
add_filter( 'posts_where', 'title_like_posts_where', 10, 2 );
function title_like_posts_where( $where, $wp_query ) {
global $wpdb;
if ( $post_title_like = $wp_query->get( 'post_title_like' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $post_title_like ) ) . '%\'';
}
return $where;
}
?>
Then:
$args = array(
'post_title_like' => $str
);
$res = new WP_Query($args);
Related Posts:
- How to Get All Posts with any post status?
- How can i get count from query post
- How to set posts per page using WP_Query()
- query_posts() in function makes global $wp_query out of sync?
- Problem with ‘post__not_in’
- Sort posts alphabetically by custom field value, insert divider between different letters
- Is it better practice to use query_posts, WP_Query, or get_posts to create various custom loops within a Page?
- Custom Post Type “Event”: chronological list of recurring events
- query_posts doesn’t order by title
- Total Count of Posts NOT in Selected Categories?
- Order posts using a custom array
- Tricky WP Query
- WordPress pagination showing same posts on each page
- Search breaks when querying main loop for category
- Using ajax with paging and a custom sub-query
- Custom loops, sticky posts, and pagination nightmare
- get_the_title outputs title until spacing; it does not get full length of title
- odd behaviours from template tags
- Having an issue doing a WP_Query with post_content and category__and
- Custom query looking at multiple custom fields and properly sorting
- Pre-existing arguments when using queries
- How to refine WP-Query with further criteria
- Run second query on page based on author of the first query
- Query Posts (post meta)
- When should you use WP_Query vs query_posts() vs get_posts()?
- When to use WP_query(), query_posts() and pre_get_posts
- posts_per_page no limit
- Get post ids from WP_Query?
- Nested meta_query with multiple relation keys
- How do I exclude a custom taxonomy from the post loop
- Some doubts about how the main query and the custom query works in this custom theme?
- Why query_posts() isn’t marked as deprecated?
- Alternative to query_posts for main loop? [duplicate]
- Wp get all the sub pages of the parent using wp query
- how to query posts by category and tag?
- Using WP_Query to Query Multiple Categories with Limited Posts Per Category?
- How to query for most viewed posts and show top 5
- Order by meta value or date?
- How do I query by post format in WordPress 3.1
- Display posts of the last 7 days
- WP Query where title begins with a specific letter
- Is there a way to exclude the content from the post variable to save on RAM usage?
- Filtering posts by post meta data
- WP_Query vs get_posts
- Querying posts from multiple sites in a network?
- How to query_posts using meta_query to orderby meta_key AND have a secondary sort by date?
- Query posts by custom taxonomy ID
- Display/query post formats
- How to query post by user role?
- Using WordPress to make a “Product Search” type navigation drilldown
- How to return results of a get_posts() in explicitly defined order
- WP_query parameters for date range
- Using OR conditions in meta_query for query_posts argument
- Custom post type archive 404’s with paginate_links
- Query Posts in a Predefined Order
- Help to condense/optimize some working code
- Custom Taxonomy not working with posts_per_page in new WP_query (pagination problem)
- Order posts by ID in the given order
- query_posts exclude a meta key
- How do I create a random post that will last for a day
- Ensuring sticky posts are retrieved first (without using two queries)?
- Query Multiple Post types each with own meta query
- Determine if more posts are available than was asked for in `query_posts()`?
- Get posts by menu ID
- Modify main WordPress loop with a parse_query filter
- Get the number of posts from the current page results
- How to exclude latest x posts from a paginated query?
- WordPress Custom Query to show posts from last x years
- Limiting query_posts to 1, regardless of sticky post?
- How to make “sticky” pages (and query by them)
- Build a content and excerpt grid loop with paging and options for # of posts
- Query posts: how to exclude results if post is in multiple categories
- Query Custom Meta Value with Increment
- query_posts ->using meta_compare / where meta value is smaller or greater or equals
- How to order posts by descending comment count on taxonomy page?
- Display Custom Post Type Fields
- How to know which one is the main query?
- Get posts by meta data OR title
- WP 5.8 “Query Loop” block: where to place custom query?
- Query posts distinct authors
- Perform query with meta_value date
- Using is_main_query to select custom post type on certain page
- WP_Query ordered by custom field that is a date string?
- get query’s query string
- Sort X categories by last update and show image
- How to get previous 10 days post from a specific date – WP Query
- Meta Query with date and time on the same Day before given time
- get_the_title($postID) OR get_the_title()?
- using post__in allow duplicate post id
- Pagination on archive.php page
- Retrieving 3 latest post from each of 5 different custom post types
- Display Posts by modifying the where clause only for my query
- WordPress Custom Query
- Best way to load page content in Fancybox popup?
- search query within custom taxonomy term, post title and meta field
- How should I intercept the main query and inject custom join / order by / group by criteria
- advice on creating a ‘related posts’ query like the one used on stackexchange
- order post my meta value m/d/y format with year as included value
- how to group custom post type posts by custom taxonomy terms
- Order by meta value, pro first, then free