Query posts by multiple custom fields

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.

Topic custom-taxonomy custom-field custom-post-types Wordpress

Category Web


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


About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.