Keyfer Mathewson
Keyfer Mathewson

Reputation: 1075

Wordpress Search Query - Meta Queries & Custom Fields

Question for all of ya. I have the current query running on my search page template, and it seems to be working fine if my search query seems to be included in the title of the post, but when I include a meta query to try and also look in another spot for the search term, it doesn't gather any results, only the same results it had before.

Second question, for some reason it still is only displaying 6 (the number of posts set in WP Admin) posts, and not listening to the query.

       <?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 } ?>

Things I've tried:

Upvotes: 4

Views: 8923

Answers (1)

Keyfer Mathewson
Keyfer Mathewson

Reputation: 1075

It seems that this is just impossible in WordPress, so I had to do this another way.

        $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
        )); ?>

Upvotes: 3

Related Questions