Query Posts | Combining multiple form inputs into query arguments to generate one filter result

admin2025-06-02  1

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.

Share Improve this question asked Mar 6, 2019 at 13:57 Jason Is My NameJason Is My Name 3782 gold badges7 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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();
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748836992a314133.html

最新回复(0)