Im trying to query my posts depending on what is in the query string in the url.
I have two query string variables. Keyword and location. I want to filter posts if just the keyword is entered, if just the location is entered or if both are entered to be more precise. I want to go off the post title. How do i structure this? Heres what i've started trying...
<?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'
)
)
);
?>
Try like these:
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 );
}