php – WordPress搜索查询 – 元查询和自定义字段

所有的问题.我有我的搜索页面模板上运行的当前查询,如果我的搜索查询似乎包含在帖子的标题中似乎工作正常,但是当我包含元查询尝试并查看另一个点时搜索词,它不收集任何结果,只有它以前的结果.

第二个问题,由于某种原因它仍然只显示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 } ?>

我试过的事情:

>硬编码值,没有任何东西.
>从’s’中删除*

最佳答案 看起来这在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
        )); ?>
点赞