Support

Account

Home Forums ACF PRO How to query posts in multiple select relationship field (serialized array)

Solved

How to query posts in multiple select relationship field (serialized array)

  • I have set up 2 custom post types (site & instructor).

    For the instructor post type, I have created a relationship custom field (identifier: site) that allows for multiple select and lists the ‘site’ post type.

    So for example, I create three sites (site1, site2 and site3).
    Then I create an instructor and I select site1 and site3 from the multiple select relationship custom field. So now one instructor is linked to two sites. Now when I view the particular instructor on the front end, I would like to see site1 and site 3 listed.

    So far, I am able to use the query below if I disable the multiple select option for the custom field and it all works. However, I would like to be able to add many sites to one instructor and if I enable multiple select, i believe the data in the DB becomes a serialized array which breaks the code below.

    $site_id = get_field('site');
    $site_query = new WP_Query("post_type=site&p=$site_id");
    if ( $site_query->have_posts() ) { while ( $site_query->have_posts() ) {$site_query->the_post();
    get_template_part( 'loop-sites' );
    }}
    wp_reset_postdata();

    Any ideas on how I can achieve this?

    I have tried changing the query to something like this but it doesn’t work for listing sites on a single instructor. This only works for listing instructors on a single site.

    $site_query = new WP_Query(
    array(
    'post_type' => 'site',
    'meta_query' => array(
    array(
    'key' => 'site',
    'compare' => 'LIKE',
    'value' => '"' . get_the_ID() . '"'
    )
    )));
    

    Thanks for any help in advance, it is really appreciated.

  • I guess the question here is…
    How would one query a post type, by a custom field that links to it from a different post type?

  • Try this, use a posts__in

    
    $sites = get_field('site')
    if ($sites) {
        $args = array(
            'post_type' => 'site',
            'post_per_page' => -1,
            'posts__in' => $sites,
            'order_by' => 'post__in'
        ); 
        $site_query = new WP_Query($args);
        if ($site_query->have_posts()) {
            while ($site_query->have_posts()) {
                $site_query->the_post();
                get_template_part('loop-sites');
            }
        }
        wp_reset_postdata();
    }
    
  • Hi John, Thank you for your help…
    Unfortunately, it didn’t work.
    It just returns all sites.

    I tried changing the posts__in line to the following (just to see if it mad a difference) but still just returns all sites instead of the 3 it’s supposed to.

    'posts__in' => array(46,89,35),

    Just for clarification, if I do the following…

    $sites = get_field('site');
    print_r($sites);

    It returns…
    Array ( [0] => 46 [1] => 89 [2] => 35 )

    posts__in doesn’t seem to be working as it should.

    Thanks again for your help on this.

  • Sorry, my mistake, it should be post__in, typo, I made it plural

    
    $sites = get_field('site')
    if ($sites) {
        $args = array(
            'post_type' => 'site',
            'post_per_page' => -1,
            'post__in' => $sites,
            'order_by' => 'post__in'
        ); 
        $site_query = new WP_Query($args);
        if ($site_query->have_posts()) {
            while ($site_query->have_posts()) {
                $site_query->the_post();
                get_template_part('loop-sites');
            }
        }
        wp_reset_postdata();
    }
    
  • Yes! I was just about to post the same thing, all works now 🙂
    Thanks so much for your help John.

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

The topic ‘How to query posts in multiple select relationship field (serialized array)’ is closed to new replies.