Querying all the events from the same artist

Hello, I have two CPT made with ACF.
Post Type 1 = artist
Post Type 2 = event

When I am on a post from an artist I want to list all events related to him.

I found the code below in this forum but it didn’t work. I have a temporary wordpress page set if it helps. The Bidrectional fields allow selecting multiple artists.

return [
 'post_type' => 'event',  //e.g. post type key
 'posts_per_page' => -1,
 'meta_query'        => array(
     array(
            'key'       => 'acf_artist, // the ACF rel field
            'value'     => get_the_id(),
            'compare'   => 'LIKE'
        )
       ),
  'order' => 'ASC',
  'orderby'=> 'title', 
];

There is an apostrophe missing in your code, the line must be

'key' => 'acf_artist', // the ACF rel field

Also, I assume your ACF field name is indeed acf_artist and not only artist, right?

Cheers

Patric

I must have deleted the apostrophe after copying it here, but it is in the code. I tried both artist and acf_artist (because when I use it in the dynamic field the value generated is {acf_artist} and still no result

Regarding the field name, you must use the field name that you have used when you created the acf field.

So check the field group in the acf settings. Maybe you can show a print screen of the acf field.

Can you also show a print screen of the query editor with your code inside it?

Sure! I can give you access to the temporary page if it helps too!


Hi

I believe in the code, the brackets for the meta query must be the round brackets () and not the square ones .

In the print screens of my code you see that I use the round ones.

The field name must be ‘artist’ as this is the name of your ACF field. So that part of the code is correct.

Cheers

Patric

Hello! I recorded a short video in case you notice where the culprit is: Filtering artists | Loom

Thanks!

Are you in the single post template?

Can you add a heading text element and make it show the dynamic post id {post_id}? I am curious to see if your template and loop have a post id.

Cheers

The other thing you can do, is to use the normal query to see if it picks up the posts…

Make a new query in the single post template for artist and use the cpt event with the acf field artist as the meta key in the meta query like this:

image

Your last tip helped! Here is what I did:
I used two queries.
image

  1. The first one Query of the Post Type “Artist”. Inside it I defined the meta query

  2. The second one is querying the Relationship field “Perfromances” (a field in the Artist post type linked to Events)
    image

Thank you so much for your help!