0

I have made two dropdown list which is a custom fields.

Custom Field 1 :- industry

Values of Custom Field 1 :- financial_services,ecommerce,insurances, etc.

Custom Field 2 :- primary_functionality

Values of Custom Field 2 :- platform_decision_engine,user_authentication,data_provider_verification etc.

And My Post Type Name is :- providers.

Now, I want to fetch posts with multiple custom fields.

So, I have tried this query. But it's not working.

$args = array(
    'numberposts'   => -1,
    'post_type'     => 'providers',
    'meta_query'    => array(
        'relation'      => 'AND',
        array(
            'key'       => 'industry',
            'value'     => 'financial_services',
            'compare'   => '='
        ),
        array(
            'key'       => 'primary_functionality',
            'value'     => 'platform_decision_engine',
            'compare'   => '='
        )
    )
);

Even I tried with for single custom field but it's also not working.

// args
$args = array(
    'numberposts'   => -1,
    'post_type'     => 'providers',
    'meta_key'      => 'industry',
    'meta_value'    => 'financial_services'
);

I'm not that much experienced in wordpress. I have read wordpress document but nothing works. I'm confused about relation and compare in this query.

3 Answers 3

0

If you are new and confused then please follow these link

https://stackoverflow.com/questions/11068795/wordpress-get-post-based-on-meta-field-content

And

https://rudrastyh.com/wordpress/meta_query.html

May be These may help You.

0

Not sure if you're using a WP_Query or WP_Meta_Query but it shouldn't matter.

$args = array(
    'numberposts'   => -1,
    'post_type'     => 'providers',
    'meta_query'    => array(
        'relation'      => 'OR',
        array(
            'key'       => 'industry',
            'value'     => 'financial_services',
            'compare'   => '='
        ),
        array('relation' => 'AND',
            array(
            'key'       => 'primary_functionality',
            'value'     => 'platform_decision_engine',
            'compare'   => '='
) ) ) );

See nested arrays

0

relation accepts AND and OR values and tells the query how you want to filter the multiple meta queries while compare is what determines each meta query is true. So in the attempt you made:

$args = array(
    'numberposts'   => -1,
    'post_type'     => 'providers',
    'meta_query'    => array(
        'relation'      => 'AND',
        array(
            'key'       => 'industry',
            'value'     => 'financial_services',
            'compare'   => '='
        ),
        array(
            'key'       => 'primary_functionality',
            'value'     => 'platform_decision_engine',
            'compare'   => '='
        )
    )
);

You're telling it to look for providers that have industry set to financial_services and their primary_functionality is set to platform_decision_engine. If it doesn't work, it's either because there is no providers with both of those settings or a provider with those settings may have their value saved as not a string but an array of strings, so the = is not finding the exact, strict match. I'm guessing it's the latter because you already showed that there's multiple different value possibilities. So maybe try something like this:

$args = array(
    'numberposts'   => -1,
    'post_type'     => 'providers',
    'meta_query'    => array(
        'relation'      => 'AND',
        array(
            'key'       => 'industry',
            'value'     => array('financial_services'),
            'compare'   => 'IN'
        ),
        array(
            'key'       => 'primary_functionality',
            'value'     => array('platform_decision_engine'),
            'compare'   => 'IN'
        )
    )
);

You can check out the WordPress codex for more info on the possible values for compare here.

1
  • This showed up in my "active" feed and I didn't realize this was asked 4 years ago, whoops!
    – AuRise
    Sep 21, 2022 at 13:39

Your Answer

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

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