我的元查询有很大的问题。我想过滤我的帖子,并且我需要查询以将post_title与我的$ _POST值进行比较。 代码:
function postsFilter(){
$args = array(
'post_type' => 'posts',
'meta_query' => array(
'relation' => 'AND'
)
);
if( isset($_POST['year']) && $_POST['year'] )
$args['meta_query'][] = array(
'key' => 'year',
'value' => $_POST['year'],
'compare' => '='
);
if( isset($_POST['theme']) && $_POST['theme'] )
$args['meta_query'][] = array(
'key' => 'theme',
'value' => $_POST['theme'],
'compare' => '='
);
if( isset($_POST['member']) && $_POST['member'] )
$args['meta_query'][] = array(
'key' => 'member_relation',
'value' => ``.$_POST['member'].``,
'compare' => 'LIKE'
);
$query = new WP_Query( $args );
echo "<script>
var posts_p = '" . json_encode( $query->query_vars ) . "',
current_page_p = " . 1 . " ,
max_page_p = " . $query->max_num_pages . ";
console.log(current_page_p, max_page_p);
</script>";
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
get_template_part('template-parts/content', 'posts');
endwhile;
if ( $query->max_num_pages > 1 )
echo '<div class="button-load-more_p btn-load-more text-center mx-auto w-100"><a class="btn btn-primary btn-md btn-with-icon fade-in full-visible load-more"><span>Load more</span></div></a></div>';
wp_reset_postdata();
else :
echo 'No publications found';
endif;
die();
}
这里存在问题:
if( isset($_POST['member']) && $_POST['member'] )
$args['meta_query'][] = array(
'key' => 'member_relation',
'value' => $_POST['member']
'compare' => 'LIKE'
);
因为我不知道如何编写查询的这一部分, 成员关系是Wordpress中的POST OBJECT类型文件,我需要将$ _POST ['member']与post_title值进行比较(也许我错了吗?)
$member = get_field('member_relation', $post_object->ID);
$member->post_title;
是否可以编写此查询?请给我任何建议或示例解决方案。
答案 0 :(得分:0)
试试这个,将您的元查询参数与帖子标题参数合并。
if( isset( $_POST['title'] ) && !empty( $_POST['title'] ) ){
$args2 = array(
's' => '"'.$_POST['title'].'"'
) ;
$args = array_merge( $args, $args2);
}