I am trying and so far failing to take two form inputs and combine them into a single post query.
I have tested these forms as two separate forms and both return results as expected, I am now trying to make them sit within the same form and have just one submit button.
Here is my code:
/* Filter Portfolio Post Results */
function portfoliofilter_filter_function(){
$args = array(
'orderby' => 'publish_date', // we will sort posts by date
'order' => 'DESC' // ASC or DESC
);
// for taxonomies / categories
if( ( isset( $_POST['sectorfilter'] ) ) || ( isset( $_POST['categoryfilter'] ) ) )
$args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( $_POST['sectorfilter'], $_POST['categoryfilter'] )
)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
A little walk through what I am trying to achieve:
Use the values of the isset( $_POST['sectorfilter'] and isset( $_POST['categoryfilter'] ), which will be category ids.
Within a query, where if one is chosen it just narrows down by one option, if the other is chosen it just narrows down by that option, but if combined the results are narrowed further to be an 'AND' relation where the results will fall into both categories.
I am trying and so far failing to take two form inputs and combine them into a single post query.
I have tested these forms as two separate forms and both return results as expected, I am now trying to make them sit within the same form and have just one submit button.
Here is my code:
/* Filter Portfolio Post Results */
function portfoliofilter_filter_function(){
$args = array(
'orderby' => 'publish_date', // we will sort posts by date
'order' => 'DESC' // ASC or DESC
);
// for taxonomies / categories
if( ( isset( $_POST['sectorfilter'] ) ) || ( isset( $_POST['categoryfilter'] ) ) )
$args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( $_POST['sectorfilter'], $_POST['categoryfilter'] )
)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
A little walk through what I am trying to achieve:
Use the values of the isset( $_POST['sectorfilter'] and isset( $_POST['categoryfilter'] ), which will be category ids.
Within a query, where if one is chosen it just narrows down by one option, if the other is chosen it just narrows down by that option, but if combined the results are narrowed further to be an 'AND' relation where the results will fall into both categories.
I needed to make the array of categories separate to each other, as this is where the 'AND' is being applied:
/* Filter Portfolio Post Results */
function portfoliofilter_filter_function(){
$args = array(
'orderby' => 'publish_date', // we will sort posts by date
'order' => 'DESC' // ASC or DESC
);
// for taxonomies / categories
if( ( isset( $_POST['sectorfilter'] ) ) || ( isset( $_POST['categoryfilter'] ) ) )
$args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_POST['sectorfilter']
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_POST['categoryfilter']
)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();