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.

I wrote a plugin Premium Posts that registers a custom taxonomy called sms_premium_posts and adds a column to the post list table in the WordPress administration area.

In that custom column, if a post has been marked Premium, the term Premium links to a filtered list of only premium posts. This link is generated using the query string:

http://www.example.com/wp-admin/edit.php?sms_premium_posts=premium

If a post is not marked as Premium, I would like the term Standard to filter the list so the table only shows posts that are NOT marked as premium (i.e. posts with no taxonomy terms).

Is there a query string for that?

Example query string:
http://www.example.com/wp-admin/edit.php?sms_premium_posts!=premium

You may ask, "Why not add the term Standard to all other posts?" My reason for not doing that is, it would require tagging all existing posts with a custom taxonomy term when the plugin is activated...which seems pretty inefficient.

Thanks in advance!

SOLUTION

Based on the answer from @s_ha_dum below, here is what I did to fake a negative query string:

function standard_term_filter( $query ) {
    if ( ! $query->is_admin || ! isset( $_GET['sms_premium_posts!'] ) )
        return $query;

    if ( 'premium' == $_GET['sms_premium_posts!'] ) {
        $taxquery = array(
            array(
                'taxonomy' => 'sms_premium_posts',
                'field'    => 'slug',
                'terms'    => 'premium',
                'operator' => 'NOT IN'
            )
        );
        $query->set( 'tax_query', $taxquery );
    }
    return $query;
}
add_action( 'pre_get_posts', array( $this, 'standard_term_filter' ) );

Note that I added this function as a method of my plugin class, which is why I used an array for the callback in add_action().

Also note the ! at the end of my query string term: sms_premium_posts!

I made my Standard term links look like this:

http://www.example.com/wp-admin/edit.php?sms_premium_posts!=premium

Since sms_premium_posts! is different than sms_premium_posts (the slug of my custom taxonomy), it works.

share|improve this question
    
Note: I can't do a custom wp_query, because I'm hooking into 'manage_posts_custom_column' to create the contents of a column in the Posts table in wp-admin. I'm hoping there is a simple query string that can be added to the URL for wp-admin/edit.php... –  Shaun Scovil May 26 '13 at 18:01

2 Answers 2

up vote 2 down vote accepted

Use a tax_query with a NOT IN operator.

$args = array(
  'post_type' => 'post',
  'tax_query' => array(
    array(
      'taxonomy' => 'sms_premium_posts',
      'field' => 'slug',
      'terms' => 'Premium',
      'operator' => 'NOT IN'
    )
  )
);
$query = new WP_Query( $args );

Now, to make that work on the post edit page via a $_GET string something like what you have in your question, you could do this:

function alter_edit_php_query_wpse_100794($qry) {
  if(isset($_GET['notin'])) {
    $qry->set(
      'tax_query',
      array(
        array(
          'taxonomy' => 'category',
          'field' => 'slug',
          'terms' => sanitize_title_with_dashes($_GET['notin']),
          'operator' => 'NOT IN'
        )
      )
    );
  }
}
add_action('pre_get_posts','alter_edit_php_query_wpse_100794');

That is very rough. You probably want more conditionals, such as is_admin, to prevent that from working globally.

Reference

http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

share|improve this answer
    
Thank you! This is just what I was looking for...great solution. –  Shaun Scovil May 27 '13 at 15:27
    
How nice is that? Even a link in the relevant code on GitHub! Finally someone who understands what the license here is about. @s_ha_dum Btw, what's your GitHub user name? Me: franz-josef-kaiser. –  kaiser Jan 30 at 2:40

It's not really possible without a tax query. You'd be better off assigning either the 'premium' or 'standard' term and never empty -- perhaps with a dropdown that holds a default value of 'Standard'.

If you'd rather not show 'Standard' over and over in the column you could always test for and hide it via the callback for manage_posts_custom_column.

See also: http://stackoverflow.com/questions/10582066/is-that-possible-to-use-in-query-string

share|improve this answer

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.