1

I want to retrieve in archive all the posts with post_status-> publish and those with post_status->pending of specific user.

That's where i'm now:

$custom_query_args = array(
    array(
        'post_status' =>'pending',
        'author' => 2),
    array(
        'post_status' =>'publish',
        'author' => 'any'),
    'relation' => 'OR' ,                    
   'paged' =>  get_query_var( 'paged' )
);
$custom_query = new WP_Query( $custom_query_args );

Any help?

3
  • Did you try it? What does it output?
    – N00b
    Commented Dec 23, 2015 at 13:30
  • only the publish posts, none of the pending (from author 2)
    – Cito
    Commented Dec 23, 2015 at 13:55
  • Nothing :( Shows only the publish
    – Cito
    Commented Dec 23, 2015 at 14:20

1 Answer 1

1

Your setup isn't supported by WP_Query.

One approach is to use the posts_where filter of WP_Query (there are other ways possible, like collecting post ID's instead from different queries.) to adjust the SQL query.

To avoid string replaces we could use for example:

$custom_query_args = [
   'post_type'   => 'post',
   'post_status' => 'publish',
];

// Add filter
add_filter( 'posts_where', 'wpse_where' );

// Query
$custom_query = new WP_Query( $custom_query_args );

// Remove filter
remove_filter( 'posts_where', 'wpse_where' );

where our custom filter callback is:

function wpse_where( $where )
{
    global $wpdb;
    return $where . " OR ( 
             {wpdb->posts}.post_author = 2 
        AND  {wpdb->posts}.post_status = 'publish'
        AND  {wpdb->posts}.post_type = 'post'           
    ) ";
}

Hopefully you can adjust this to your needs.

3
  • Thanks. I didn't know that i cant do it by using Wp_query.
    – Cito
    Commented Dec 23, 2015 at 14:34
  • I made it by collecting all ids (with get_posts), then merged and finally wp_query with 'post__in'
    – Cito
    Commented Dec 23, 2015 at 14:35
  • Glad to hear you worked it through @Cito
    – birgire
    Commented Dec 23, 2015 at 17:33

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.