0

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', 'CPT1');
    $results_posts= get_field('cpt1_field_for_cpt2',$post_id,false);
    $meta_query[] = [
         'post_in' => $results_posts->ID ,
    ];
    $query->set('meta_query', $meta_query);
});
2
  • Where did $post_id come from? I don't see it being passed to the function in any way, check that to see if it contains what you expect, probably not. Sep 7, 2022 at 6:13
  • 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' );
    – AKEAmazan
    Sep 9, 2022 at 0:04

1 Answer 1

0

The issue can be solved 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' );

Your Answer

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

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