我正在尝试根据网址中查询字符串中的内容查询帖子。
我有两个查询字符串变量。关键字和位置。如果只输入关键字,或者只输入位置,或者输入的都更精确,我想过滤帖子。我想取消帖子标题。我该如何构造?这是我开始尝试的东西...
<?php
echo $keywords = $_GET["search_keywords"];
echo $location = $_GET["search_location"];
// the query to set the posts per page to 3
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 8,
'paged' => $paged ,
'post_type' => 'job_listing',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_job_title',
'value' => $keywords,
'compare' => 'LIKE'
)
)
);
?>
答案 0 :(得分:0)
尝试这些:
if( !empty( get_query_var( 'search_keywords' ) ) ){
$meta_query[] = array( 'key' => '_job_title', 'value' => get_query_var( 'search_keywords' ), 'compare' => 'LIKE' );
}
if( !empty( get_query_var( 'search_location' ) ) ){
$meta_query[] = array( 'key' => '_job_title', 'value' => get_query_var( 'search_location' ), 'compare' => 'LIKE' );
}
if( count( $meta_query ) > 1 ){
$meta_query['relation'] = 'AND';
}
if( count( $meta_query ) > 0 ){
$query->set( 'meta_query', $meta_query );
}