0

I would really appreciate help or any lead. I have checkboxes with which I'm collecting data for the filters. Here is the code for checking if variable is set:

if ( isset($_GET['f']) && $_GET['f'] == '1')  {$face = 'facebook';} else {$face = '';}
        if ( isset($_GET['t']) && $_GET['t'] == '1' ) {$twitter = 'twitter';} else {$twitter = '';}
        if ( isset($_GET['te']) && $_GET['te'] == '1' ) {$telegram = 'telegram';} else {$telegram = '';}
        if ( isset($_GET['r']) && $_GET['r'] == '1' ) {$reddit = 'reddit';} else {$reddit = '';}
        if ( isset($_GET['e']) && $_GET['e'] == '1' ) {$email = 'email';} else {$email = '';}
        if ( isset($_GET['ph']) && $_GET['ph'] == '1' ) {$phone = 'phone';} else {$phone = '';}
        if ( isset($_GET['b']) && $_GET['b'] == '1' ) {$bitcointalk = 'bitcointalk';} else {$bitcointalk = '';}
        if ( isset($_GET['k']) && $_GET['k'] == '1' ) {$kyc = 'kyc';} else {$kyc = '';}
        if ( isset($_GET['y']) && $_GET['y'] == '1' ) {$youtube = 'youtube';} else {$youtube = '';}
        if ( isset($_GET['l']) && $_GET['l'] == '1' ) {$linkedin = 'linkedin';} else {$linkedin = '';}          
        if ( isset($_GET['eth']) && $_GET['eth'] == '1' ) {$ethereum = 'ethereum';} else {$ethereum = '';}
        if ( isset($_GET['neo']) && $_GET['neo'] == '1' ) {$neo = 'neo';} else {$neo = '';}
        if ( isset($_GET['xml']) && $_GET['xml'] == '1' ) {$stellar = 'stellar';} else {$stellar = '';}
        if ( isset($_GET['waves']) && $_GET['waves'] == '1' ) {$waves = 'waves';} else {$waves = '';}
        if ( isset($_GET['kmd']) && $_GET['kmd'] == '1' ) {$komodo = 'komodo';} else {$komodo = '';}
        if ( isset($_GET['own']) && $_GET['own'] == '1' ) {$own = 'own';} else {$own = '';}

I have 2 custom taxonomies: filters and platform. Query works if I'm filtering by only 1 custom taxonomy. But recently I have added the second one (platform). Here is the code I'm trying to use:

$tax_query_args = array(
                    'relation'      => 'OR',
                    array(
                        'taxonomy' => 'filters',
                        'field'    => 'slug',
                        'terms'    => array ($face, $twitter, $telegram, $reddit, $email, $phone, $bitcointalk, $kyc, $youtube, $linkedin),
                        'operator' => 'IN'
                    ), 

                    array(
                        'taxonomy' => 'platform',
                        'field'    => 'slug',
                        'terms'    => array ($ethereum, $neo, $stellar, $waves, $komodo, $own),
                        'operator' => 'IN'
                    ), 

                );

But it doesn't work. All results are returned.

I would appreciate any help.

Thank you.

0

Based on your code, if the url is:

http://example.com/page/?eth=1&t=1

Then the resulting query will be:

$tax_query_args = array(
    'relation'      => 'OR',
    array(
        'taxonomy' => 'filters',
        'field'    => 'slug',
        'terms'    => array( '', 'twitter', '', '', '', '', '', '', '', '' ),
        'operator' => 'IN'
    ), 
    array(
        'taxonomy' => 'platform',
        'field'    => 'slug',
        'terms'    => array ( 'ethereum', '', '', '', '', ''),
        'operator' => 'IN'
    ), 
);

While the empty variables are a bit messy, WordPress will ignore them. So really you're just querying posts that have the 'twitter' filter or 'ethereum' platform. So if that's what you want, the code is fine.

If it's not working then either the slugs or taxonomy names are incorrect, or there's something else wrong with the query that you are using $tax_query_args in. It's impossible to say without seeing more, but what you have here is fine.

With all that said, I want to suggest a considerably cleaner way to approach this.

You mentioned checkboxes, and based on your use of $_GET I'm assuming your form looks like this:

<input name="t" value="1" type="checkbox">
<input name="f" value="1" type="checkbox">
<input name="eth" value="1" type="checkbox">
<input name="neo" value="1" type="checkbox">

Doing it this way means you need that huge block of code just to figure out which taxonomy terms were selected.

I suggest you change the form to work like this:

<input name="filter[]" value="twitter" type="checkbox">
<input name="filter[]" value="facebook" type="checkbox">
<input name="platform[]" value="ethereum" type="checkbox">
<input name="platform[]" value="neo" type="checkbox">

Now all you need to do to query the right taxonomy terms is:

$filters = isset( $_GET['filters'] ) ? $_GET['filters'] : array();
$platform = isset( $_GET['platform'] ) ? $_GET['platform'] : array();

$tax_query_args = array(
    'relation'      => 'OR',
    array(
        'taxonomy' => 'filters',
        'field'    => 'slug',
        'terms'    => $filters,
        'operator' => 'IN'
    ), 
    array(
        'taxonomy' => 'platform',
        'field'    => 'slug',
        'terms'    => $platform,
        'operator' => 'IN'
    ), 
);
| improve this answer | |
  • Hi Jacob, and thank you for the detailed explanation and answer. I have just figured out where is the problem in my code. It should be $tax_query_args[] instead of $tax_query_args – Ante Medic Apr 13 '18 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.