Search code examples
wordpressadvanced-custom-fieldscustom-post-typeelementor

Display ACF object field data using Elementor Custom Query


Hello i'm working on a website using elementor pro + ACF. I have create a custom post type (CPT1) with ACF object field that take another CPT2 as values. I would like to display on my CPT1 page all CPT2 linked. How can I used Custom Query to display this? I tried to write this but still not working.

add_action('elementor/query/13600', function ($query) {
$query->set('post_type', ['CPT2']);
$meta_query[] = [
    'post__in' => get_field(['cpt1_field_for_cpt2']),
];
$query->set('meta_query', $meta_query);});

Solution

  • Problem solves with this code :

    function my_cpt_query( $query ) {
        $myPostObjetField= get_field('cpt1_field_for_cpt2',$post_id,false);
        foreach( $myPostObjetField as $post_field_item_value ) {
                $ids[] = (int) $post_field_item_value;
        }
        $query->set( 'post_type', 'CPT2');
        $query->set( 'post__in', $ids);
    }
    add_action( 'elementor/query/13600', 'my_cpt_query' );