所有的问题。我有我的搜索页面模板上运行的当前查询,如果我的搜索查询似乎包含在帖子的标题中似乎工作正常,但是当我包含元查询尝试并查看另一个点时搜索词,它不会收集任何结果,只会得到与以前相同的结果。
第二个问题,由于某种原因,它仍然只显示6(WP Admin中设置的帖子数)帖子,而不是收听查询。
<?php // WP_User_Query arguments
$search_term = get_search_query();
$args = array (
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'nopaging' => true,
's' => '*'.$search_term.'*',
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
$wp_course_query = new WP_Query($args);
// Get the results
$courses = $wp_course_query; ?>
<?php // Check for results
if (!empty($courses->get_posts())) { ?>
<ul class="course-list">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<li> <?php the_title(); ?> </li>
<?php endwhile; endif; wp_reset_query(); ?>
</ul>
<?php } else { ?>
<p>No courses match that query</p>
<?php } ?>
我尝试的事情:
答案 0 :(得分:2)
似乎这在WordPress中是不可能的,所以我不得不采取另一种方式。
$search_term = get_search_query();
$args = array (
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'nopaging' => true,
's' => $search_term
);
$args2 = array (
'post_type' => 'courses',
'posts_per_page' => -1,
'nopaging' => true,
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
$courses1 = get_posts($args);
$courses2 = get_posts($args2);
$merged = array_merge($courses1, $courses2);
$post_ids = array();
foreach ($merged as $item) {
$post_ids[] = $item->ID;
}
$unique = array_unique($post_ids);
$posts = get_posts(array(
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'post__in' => $unique,
'posts_per_page' => -1
)); ?>