重新访问并简化答案:
您可以尝试:
$args = [
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'category_name\' => \'projects\',
\'_name__like\' => \'proj*\' // <-- our new input argument!
];
$my_query = new WP_Query( $args );
我们在这里创建了
_name__like
输入参数。它支持通配符
*
, 例如:
\'_name__like\' => \'a*b*\'
请注意,草稿帖子没有
post_name
在发布前设置。
我们使用以下插件来支持此新参数:
/**
* 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 );