Support

Account

Home Forums General Issues Problem Query Select Multiple Field

Solved

Problem Query Select Multiple Field

  • Hello,
    I have a problem recovering posts from my custom post type whose field name is ‘activity’. This is an acf field of multiple select type.
    Here is my query to try to recover my posts:

    $posts = get_posts(array(
        'posts_per_page'	=> 1000,
        'post_type'			=> $post_type,
    //    'paged' => $paged,
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'activite',
                'value' => array('Coiffure/Esthétique'),
                'compare' => 'IN'
            ),
        )));

    On the other hand it works to look for a single post with this query:

    $posts = get_posts(array(
        'posts_per_page'	=> 1000,
        'post_type'			=> $post_type,
    //    'paged' => $paged,
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'activite',
                'value' => 'Coiffure/Esthétique',
                'compare' => 'LIKE'
            ),
        )));

    Thanks for your help

  • You cannot to an “IN” query with an ACF select field. ACF saves this value as a serialized array (A string).

    
    $posts = get_posts(array(
        'posts_per_page'	=> 1000,
        'post_type'			=> $post_type,
    //    'paged' => $paged,
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'activite',
                'value' => 'value 1',
                'compare' => 'LIKE'
            ),',
            array(
                'key' => 'activite',
                'value' => 'value 2',
                'compare' => 'LIKE'
            ),
        )));
    
  • For more information see this page https://www.advancedcustomfields.com/resources/query-posts-custom-fields/. The query you used depends on the type of field and how ACF stores that value in the database.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Problem Query Select Multiple Field’ is closed to new replies.