Skip to content

Instantly share code, notes, and snippets.

@cdmz
Created October 7, 2015 14:28
Show Gist options
  • Save cdmz/458d996287dc217a8cb8 to your computer and use it in GitHub Desktop.
Save cdmz/458d996287dc217a8cb8 to your computer and use it in GitHub Desktop.
search post like title wordpress
<?php
add_filter( 'posts_where', 'like_title', 10, 2 );
function like_title( $where, &$wp_query ) {
global $wpdb;
if ( $title = $wp_query->get( 'title' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $title ) ) . '%\'';
}
return $where;
}
$search_query = array(
'post_type'=> array(
'post'),
'title' => 'Thinkers50',
'posts_per_page'=> 9,
);
$wp_query = new WP_Query($search_query);
@erikdemarco
Copy link

If the post_title is "search post like title wordpress" and user query is "wordpress post search" it will not found Because it will search for the exact match of 'wordpress post search' instead searching word one by one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment