我尝试使用多个帖子标题获取 wp 查询,但帖子标题或 s 仅接受一个参数。
这是我的代码:
$post_title = array(
'book',
'car',
'bike'
);
$conditions = array(
'posts_per_page' => 9,
'paged' => $paged,
'post_type' =>'custom',
'order' => 'DESC',
's' => $post_title,
'post_status' => "publish",
);
$the_query = new WP_Query( $conditions );
无法要求
WP_Query
搜索多个帖子标题。您的数组值 s
参数会查找包含所有三个单词的标题。
如果您碰巧知道帖子的别名(
$post->name
值),您可以使用
'post_name__in' => array ( 'book', 'car', 'bike' ),
寻找它们。但很难从帖子名称中预测 slugs 的值。
另一种可能性:使用多个单独的 WP_Query 操作,每个标题一个,并获取帖子的帖子
ID
值。然后使用
'post__in' => array (id, id, id, id),
构建一个 WP_Query 来获取匹配的帖子。
$post_title = array(
'book',
'car',
'bike'
);
$multiple_title = array(); // array for store multiple arguments for multiple title
foreach ( $post_title as $title ){
$multiple_title[] = array(
'key' => 'title',
'value' => $title,
'compare' => 'LIKE'
)
}
$args = array(
'post_type' => 'post', // or 'page', or a custom post type
'post_status' => 'publish',
'posts_per_page' => -1, // Retrieves all matching posts
'meta_query' => array(
'relation' => 'OR',
$multiple_title,
)
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Display your posts here
}
}
wp_reset_postdata();