1

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. How to pass array in values field because it's give me unexpected result.

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

$search_args = array(
        'posts_per_page' => -1,
        'post_type'     => 'providers',
        'post_status'   => 'publish',
        'orderby'       => 'title',
        'order'         => 'ASC',
        'meta_query'    => array(
            'relation'      => 'OR',
            array(
                'key'       => 'industry',
                'value'     => $industry,
                'compare'   => 'IN'
            ),
            array(
                'key'       => 'primary_functionality',
                'value'     => array('platform_decision_engine'),
                'compare'   => 'IN'
            )
        ),
        'suppress_filters' => true
    );  
}
    $the_query_search = new WP_Query( $search_args );

This Query is not getting expected result.

EDIT :

[query] => Array
        (
            [posts_per_page] => -1
            [post_type] => providers
            [post_status] => publish
            [meta_query] => Array
                (
                    [0] => Array
                        (
                            [key] => industry
                            [value] => Array
                                (
                                    [0] => ecommerce
                                )

                            [compare] => IN
                        )
                )    
        )

Getting this array but data is not according to ecommerce.

Add Field pic : enter image description here

4
  • What result did you get and what did you expect? "Not working" is kind of broad...
    – cjbj
    Apr 27, 2018 at 11:01
  • @cjbj i updated my code.
    – WebRence
    Apr 27, 2018 at 11:17
  • Can you share how your custom fields are defined? (code or plugin or something). To get a better idea.
    – rozklad
    Apr 27, 2018 at 11:38
  • @rozklad, i upload custom field list. it's a plugin (Custom Post Type UI).
    – WebRence
    Apr 27, 2018 at 12:00

1 Answer 1

0

If you have array based values in custom fields defined by ACF (as i can see from your screenshot), you will want to write meta query with LIKE as compare instead of IN.

I'm assuming your "providers" post type looks something like this: enter image description here

Here is your example written with LIKE compare:

<?php

    $search_args = array(
        'posts_per_page' => -1,
        'post_type'     => 'providers',
        'post_status'   => 'publish',
        'orderby'       => 'title',
        'order'         => 'ASC',
        'meta_query'    => array(
            'relation'      => 'OR',
            array(
                'key'       => 'industry',
                'value'     => 'financial_services',
                'compare'   => 'LIKE'
            ),
            array(
                'key'       => 'primary_functionality',
                'value'     => 'platform_decision_engine',
                'compare'   => 'LIKE'
            )
        ),
        'suppress_filters' => true
    );  

    $the_query_search = new WP_Query( $search_args );

    while($the_query_search->have_posts()) : $the_query_search->the_post();

        echo get_the_title() . '<br>';

    endwhile;

?>

Refer to https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ for more information on custom field meta queries.

2
  • thank you for a response, I try your code but the same issue. Not getting proper data.
    – WebRence
    Apr 27, 2018 at 12:44
  • its difficult to tell whats the problem. i tested it on a clean wp installation se.rozklad.me with some testing providers set up as seen on the screenshot - fields industry and primary_functionality being type Checkbox and it returns results as expected.
    – rozklad
    Apr 27, 2018 at 13:18

Your Answer

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

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