admin管理员组

文章数量:1122846

I have a CPT that contains Employee Bio information. Because we don't want to create accounts in wp_users for everyone, this data is implemented as a "regular" CPT living in wp_posts. Let's say I have a row with ID 250 for employee Scott.

We also have normal blog posts. Since we're not using regular WP users, we can't take advantage of the usual metadata and relationships to associate authors with posts. So instead, we have a custom field, "af_author", created with an ACF Relationship field type. When we create a post, we choose an author from the list of Employee Bio objects.

On the page that displays the bio information for an employee, we want to include a list of posts that the employee has authored. So when I'm looking at the Employee Bio page for Scott (ID = 250), I want to do a query loop to retrieve all Posts that have been written by ID = 250. Who the post is written by is stored in the related custom field "af_author" for each post. The field itself is an array of IDs; ID 250 would need to be "IN" the array.

I'm having trouble figuring out how to code the meta query for this. The code below is executed while creating the Employee Bio page, so if employee Scott is being displayed, get_the_ID() would return 250. I have tried the following code with no success:

    $new_query_args = [
        'meta_query' => [
            [
                'key'     => 'af_author',
                'value'   => get_the_ID(),
                'compare' => 'IN',
            ]
        ]
    ];

    $query_args = array_merge( $query_args, $new_query_args );

How do I make this work?

I have a CPT that contains Employee Bio information. Because we don't want to create accounts in wp_users for everyone, this data is implemented as a "regular" CPT living in wp_posts. Let's say I have a row with ID 250 for employee Scott.

We also have normal blog posts. Since we're not using regular WP users, we can't take advantage of the usual metadata and relationships to associate authors with posts. So instead, we have a custom field, "af_author", created with an ACF Relationship field type. When we create a post, we choose an author from the list of Employee Bio objects.

On the page that displays the bio information for an employee, we want to include a list of posts that the employee has authored. So when I'm looking at the Employee Bio page for Scott (ID = 250), I want to do a query loop to retrieve all Posts that have been written by ID = 250. Who the post is written by is stored in the related custom field "af_author" for each post. The field itself is an array of IDs; ID 250 would need to be "IN" the array.

I'm having trouble figuring out how to code the meta query for this. The code below is executed while creating the Employee Bio page, so if employee Scott is being displayed, get_the_ID() would return 250. I have tried the following code with no success:

    $new_query_args = [
        'meta_query' => [
            [
                'key'     => 'af_author',
                'value'   => get_the_ID(),
                'compare' => 'IN',
            ]
        ]
    ];

    $query_args = array_merge( $query_args, $new_query_args );

How do I make this work?

Share Improve this question edited Aug 2, 2024 at 18:01 scott8035 asked Aug 2, 2024 at 17:40 scott8035scott8035 2302 silver badges10 bronze badges 3
  • 1 can you expand the code in your question? It's unclear what get_the_ID is going to return or the context. Also keep in mind that meta_query is expensive and doesn't scale well, it would be much faster ( possibly orders of magnitude faster ) to use a taxonomy rather than post meta ( aka ACF field ). Also while ACF is offtopic here, meta_query is on topic – Tom J Nowell Commented Aug 2, 2024 at 17:45
  • I added "The code below is executed while creating the Employee Bio page, so if employee Scott is being displayed, get_the_ID() would return 250." to the description preceding the code. – scott8035 Commented Aug 2, 2024 at 18:03
  • can you expand your code? How are query_args created and what does it contain? Is this inside or outside the loop? Is your author page a custom template that uses a WP_Query to fetch the author or is it a main loop in a standard template? When you say it's 250 is that what it should be, or have you checked and seen the value directly to confirm it? – Tom J Nowell Commented Aug 3, 2024 at 9:03
Add a comment  | 

1 Answer 1

Reset to default 0

Based on https://stackoverflow.com/questions/42011047/wordpress-query-by-acf-relationship-field, I was able to fix this by changing the 'compare' => 'IN' to 'compare' => 'LIKE'. So the resultant code looks like this:

$new_query_args = [
    'meta_query' => [
        [
            'key'     => 'af_author',
            'value'   => get_the_ID(),
            'compare' => 'LIKE',
        ]
    ]
];

$query_args = array_merge( $query_args, $new_query_args );

I'm not exactly sure why this works, but it fixed the problem.

本文标签: custom post typesMeta query with ACF relationship field