0

This is on the second page of a filter that I am making, on the first page the user is able to select check-boxes. The values of the check-boxes get passed to the second page by parameters in the URL: filter-result/?mytaxonomy=myterm&mytaxonomy=myotherterm How to form an array of this data to use in a (WP) query?

I'm able to display the data from the URL by doing this:

    if( isset( $_GET['mytaxonomy'] ) ){
        foreach( $_GET['mytaxonomy'] as $term ){
            echo $term . '<br>';
        }
    }

I am also able to query posts (custompost-type):

    $query = new WP_Query( array(
        'post_type' => 'mycustomposttype',
        'tax_query' => array(
            array(
                'taxonomy' => 'mytaxonomy',
                'field' => 'slug',
                'terms' => array( 'myterm', 'myotherterm' ),
                'operator' => 'AND',
            ),
        ),
    ) );

I want to pass the data from $_GET['mytaxonomy'] to 'terms' => array( *inside here* ).

When I use print_r ($_GET['mytaxonomy']); the result is Array ( [0] => myterm ), All correct. I guess I just need to form the array to 'a', 'b' to work in the WP query. How can I achieve this?

2 Answers 2

1

For others that may question how to go about this: this was an easy fix. Because as @Wodka suggested, I formed the link with [] brackets like such: <input type="checkbox" name="mytaxonomy[]" value="myterm"> and because $_GET['mytaxonomy'] outputs an array() itself, I was able to just drop it in like below.

'terms' => $_GET['mytaxonomy'],

Which resulted in:

$query = new WP_Query( array(
    'post_type' => 'mycustomposttype',
    'tax_query' => array(
        array(
            'taxonomy' => 'mytaxonomy',
            'field' => 'slug',
            'terms' => $_GET['mytaxonomy'],
            'operator' => 'AND',
        ),
    ),
) );
Sign up to request clarification or add additional context in comments.

Comments

0

You can pass array to php like filter-result/?mytaxonomy[]=myterm&mytaxonomy[]=myotherterm - the syntax you used is from the java world (and not applicable to php)

1 Comment

That is actually how I did form the URL. On the first page, there is a form with: <label><input type="checkbox" name="mytaxonomy[]" value="myterm">myterm</label>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.