Revisited and simplified answer:
You can try:
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'projects',
'_name__like' => 'proj*' // <-- our new input argument!
];
$my_query = new WP_Query( $args );
where we’ve created the _name__like
input argument. It supports wildcard *
, for example:
'_name__like' => 'a*b*'
Note that draft posts don’t have post_name
set before they’re published.
We use the following plugin to support this new argument:
/**
* Plugin Name: Support for post name like in WP_Query
* Description: Uses the _name__like argument and supports wildcard *.
* Plugin URI: http://wordpress.stackexchange.com/a/136758/26350
* Author: Birgir Erlendsson (birgire)
* Version: 0.0.1
*/
add_filter( 'posts_where', function( $where, $q )
{
if( $name__like = $q->get( '_name__like' ) )
{
global $wpdb;
$where .= $wpdb->prepare(
" AND {$wpdb->posts}.post_name LIKE %s ",
str_replace(
array( '**', '*' ),
array( '*', '%' ),
mb_strtolower( $wpdb->esc_like( $name__like ) )
)
);
}
return $where;
}, 10, 2 );