1

i have in all of my posts an additional custom field (acf user field) in case the post has a co-author.

All users have their own author-page where all their posts are displayed. No problem so far.

But what i now want is a query where i get all posts where the author is the actual author and all posts where he/she is just the co-author in the custom field.

My code is pretty simple so far, but did not actually work. I guess the 'relation' parameter is only for comparing meta query-subarrays.

$args = array(
   'post_type'              => array( 'post' ),
   'posts_per_page'         => 20,
   'relation'               => 'OR',
       array(
           'author'                 => $author_ID,
            ),
       array(
           'meta_key'                => 'authorfeld',
           'meta_value'              => $showme[ID], // This is the authors id from the custom field
            )
    );

Thank you

1

I found some hints here: Can i merge 2 new WP_Query($variable) 's?

So i created two separate queries and merged them into a new array:

$wp_query = new WP_Query();
$wp_query->posts = array_merge( $query1->posts, $query2->posts ); 

foreach($wp_query->posts as $post) :
setup_postdata( $post );

And then the usual stuff.

0

Real quick, are you sure you spelled the name of the meta_key correctly? It says "authorfeld" in your example, but maybe it's supposed to be "authorfield"? Also $showme[ID] would need to be $showme['ID'] unless ID is a defined variable.

In any case, your $args array is not quite correct. Try these examples:

// Get the posts from this author, but not this coauthor.
$author_not_coauthor = array(
   'post_type'              => array( 'post' ),
   'posts_per_page'         => 20,
   'author'                 => $author_ID,
   'meta_query' => array(
        array(
            'key' => 'authorfeld',
            'value' => $showme['ID'],
            'compare' => 'NOT LIKE',
        ),
   ),
);

// Get the posts from this coauthor, but not this author.
$coauthor_not_author = array(
   'post_type'              => array( 'post' ),
   'posts_per_page'         => 20,
   'author__not_in'         => array( $author_ID ),
   'meta_query' => array(
        array(
            'key' => 'authorfeld',
            'value' => $showme['ID'],
        ),
   ),
);
  • Thx for your reply, but i think you missunderstood me. I want the query to get all the posts where the author is the mainauthor AND all the posts where he is the co-author (defined by the meta_query). Your examples give me just one of it and when i combine the query the query it gives me only the posts where he would be both. – Steve Sep 1 '17 at 19:14
  • In that case you ought to be able to use the first example, but comment out the "compare" parameter in the meta query. – James Sep 2 '17 at 12:58
  • Nope, doesnt work. Unfortunately. I guess i have to go with two separate queries. – Steve Sep 4 '17 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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