php - Wordpress Search Query - Meta Queries & Custom Fields -
question of ya. have current query running on search page template, , seems working fine if search query seems included in title of post, when include meta query try , in spot search term, doesn't gather results, same results had before.
second question, reason still displaying 6 (the number of posts set in wp admin) posts, , not listening 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); // results $courses = $wp_course_query; ?> <?php // check 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 query</p> <?php } ?>
things i've tried:
- hard coding value, nothing there.
- removing * 's'
it seems impossible in wordpress, had 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 $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 )); ?>
Comments
Post a Comment