Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

It seem wp_query have the problem where it does not accept more than 1 negative value in author argument. for example:

$args = array('author=-2,-3,-4'); 
$newquery = WP_Query($args); 

This grab everything else. Simply doesn't work.

I found this trac, but seem still no official patch for this.

Any suggestion?

share|improve this question
add comment

3 Answers

You are right, it is not possible to pass an array with author IDs to exclude them. The reason is in wp-includes/query.php at line 2294 - 2298

if ( strpos($q['author'], '-') !== false ) {
  $eq = '!=';
  $andor = 'AND';
  $q['author'] = explode('-', $q['author']);
  $q['author'] = (string)absint($q['author'][1]); // this line select the first ID and only this ID
} else {
  $eq = '=';
  $andor = 'OR';
  }

You have to get all author IDs, then exclude the unwanted authors and request the posts from the remaining authors

// array for user IDs
$users   = array();

// roles to include
$roles   = array( 'author', 'editor', 'contributor', 'administrator', 'superadmin' );

// user IDs to exclude
$exclude = array( 2,3,5 );

// get all users
$_users = get_users();

foreach ( $_users as $user ) {

    // do not add the user ID if the users role does not match or the users ID is excluded
    if ( in_array( $user->ID, $exclude ) || ! in_array( $user->roles[0], $roles ) )
        continue;

    // add user
    array_push( $users, $user->ID );

}

$args = array('author=' . implode( ',', $users ); 
$newquery = WP_Query( $args );
share|improve this answer
add comment

I didn't realize it before, but it seems that you currently can't exclude multiple authors posts using WP_Query, I've updated the Codex to be less misleading.

Here is a way to exclude posts from multiple authors using the $wpdb class:

<?php

   $results = $wpdb->get_results(
   "Select * FROM $wpdb->posts
   WHERE post_type = 'post'
   AND post_status = 'publish'
   AND post_author <> 2
   AND post_author <> 3", 
   OBJECT
   );// gets posts, that are published, and exclude posts written by authors 2 and 3

   foreach($results as $result)
   {
   echo "<h2>".$result->post_title."</h2>";
   }

?>

share|improve this answer
    
I put it in an array because i have other parameters need to be filled in. Besides, this query work for the positive author id, it suppose to work for the negative author id as well. –  dev-jim Sep 26 '12 at 12:14
    
Please see updated answer –  Ben Sep 27 '12 at 18:04
    
I don't think this is a good solution, because it completely short-circuits all of the business logic and brains within WP_Query. You're much better using the 'posts_where' filter and appending to the existing WHERE clause (once you do the appropriate checking to be sure you're modifying only those queries you're interested in modifying). Your solution works, but is missing out on any of the "brains" that WordPress already provides. Sticking with filters is usually a much better bet. –  Tom Auger Apr 25 '13 at 17:16
add comment

"UNTESTED" but could you do it this way?

 function filter_pre_get_posts( $query ) {

// If this is the blog posts index,
// and if this is the main query,

if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( 'author', -2,-3,4);
    }
 }
 add_action( 'pre_get_posts', 'filter_pre_get_posts' );

This is a function I picked up here for excluding categories. Not sure if it could be used to exclude by author but it is far better than writing a custom query if it works

share|improve this answer
    
I using the query in a plugin, so this method won't help. anyway thanks for the tips. –  dev-jim Sep 26 '12 at 11:59
1  
You can use pre_get_posts in a plug-in... –  Stephen Harris Sep 26 '12 at 16:30
    
@Stephen Harris, I use the query for running at the background. How do I hook it when it is not depending on front end pages? –  dev-jim Sep 26 '12 at 18:54
    
I'm not saying that you should use it for your specific purpose (I don't know what that is). I was just clarifying that pre_get_posts is available to plugins. I didn't want anyone to misunderstand your comment :) –  Stephen Harris Sep 26 '12 at 19:05
    
Sadly, this will not work for a number of reasons 1. WP_Query->set() only takes 2 arguments - it will completely ignore the 3rd and 4th arguments; you probably meant $query->set( 'author', '-2, -3, -4' );, 2. however that will not work either based on a limitation on how the author query variable is currently being handled in core. Follow (and add your cc) to this bug/enhancement request: core.trac.wordpress.org/ticket/16854 –  Tom Auger Apr 25 '13 at 17:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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