Query posts by multiple custom fields

admin2025-01-08  3

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.

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.

Share Improve this question asked Apr 21, 2018 at 9:03 BhavinBhavin 1013 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267925a1232.html

最新回复(0)