0

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?

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 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 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 at 9:03

1 Answer 1

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.

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.