0
votes

I was wondering if someone could help me out. I can't seem to get this case working (since I am a noob in PHP).

Case: My wordpress site has two 'post types':

  • Webinar
  • Spreker (speaker in dutch)

Via Advanced Custom Posts i've set up a relationship field in the webinar post type. So I can pick the speakers related to that particular post.

I use elementor and need to use the elementor's post widget to display the related speaker(s) via the single webinar page template. This has to be done by a custom query. In elementor you can set a custom query ID. This calls for a php query code.

Now I can't seem to get this query array code right.

Elementor documentation about custom queries: https://developers.elementor.com/custom-query-filter/#Using_the_Custom_Filter

Some related answers on internet (but can't still seem to get it working): Custom Query Filter for Elementor Posts by relationship field (ACF)

The custom query ID i use in Elementor is 'Spreker_filter'

My code:

add_action( 'elementor/query/spreker_filter', function( $query ) {
// Get current meta Query
$meta_query = $query->get( 'meta_query' );

// If there is no meta query when this filter runs, it should be initialized as an empty array.
if ( ! $meta_query ) {
$meta_query = [];
}

// Append our meta query
$meta_query[] = [
'key' => 'sprekers',
'value' => '"' . get_the_ID() . '"',
'compare' => 'in',
];
$query->set( 'meta_query', $meta_query );
} );
1

1 Answers

1
votes

It looks like i was having the same problem. I've resolved it with this custom query

add_action( 'elementor/query/retrieveLinkedCpts', function( $query ) {
    $ids = get_field( 'my_relationship_field', false, false );
    $query->set( 'post__in', $ids );
});

source : https://github.com/elementor/elementor/issues/4916

Let me know if it works