I want to perform a search in my wordpress site where I have a custom post (cp_course
) with some custom fields (course_code
, course_iteration
). Course_iteration
custom field is a Repeater
containing these date sub_fields
: course_iteration_start
and course_iteration_start
.
So this is what I have tried so far:
// filter
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'course_iterations_%", "meta_key LIKE 'course_iterations_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
$args = array(
'post_type' => 'cp_course', 'numberposts' =>-1,'orderby' => 'ID', 'order' => 'ASC', 's' => $searchterm,
'meta_query' =>
array(
'relation' => 'AND',
array(
'key' => 'course_code',
'value' => $code,
),
array(
'key' => 'course_duration',
'value' => $duration,
),
array(
'key' => 'course_iterations_%_course_iteration_start',
'compare' => 'between',
'type' => 'numeric',
'value' => array($date_from, $date_to),
)
)
);
$course = get_posts($args);
<div class="page_title">
<h3>Courses</h3>
</div>
<?php foreach ($course as $post):setup_postdata($post);?>
<a href="#"><?php the_title();?></a>
<?php endforeach;wp_reset_postdata();?>
But with no luck. Can anyone guide me on how to solve this? I've spent so many days on it without managing to get it to work and its so frustrating. If it's not easy is there a plugin that can achieve that?
%
to$
fix that is mentioned in the docs. Change$where = str_replace("meta_key = 'course_iterations_%", "meta_key LIKE 'course_iterations_%", $where);
to$where = str_replace("meta_key = 'course_iterations_$", "meta_key LIKE 'course_iterations_%", $where);
and'key' => 'course_iterations_%_course_iteration_start',
to'key' => 'course_iterations_$_course_iteration_start',
– rickibarnes