1

I want the defult CPT listing to include not all posts but only some, according to a specific taxonomy. is there a way to do that ?

1 Answer 1

2

This trick should help you...

<?php
add_action('pre_get_posts', 'filter_posts_list');
function filter_posts_list($query)
{
    //$pagenow holds the name of the current page being viewed
    global $pagenow;
    $post_type = $query->get('post_type');

    //Check the post type and whether its the list page, then set the taxonomy
    if($post_type == 'my_cpt' && 'edit.php' == $pagenow)
    {
        //global $query's set() method for setting the taxonomy
        $query->set('my_taxonomy', 'my_taxonomy_value');
    }
}
?>

You can use any WP_Query parameters with the set method to modify this solution. For a detailed explanation, hit this.

2
  • one error in the if up there. couldn't edit coz it's just one char Commented Mar 7, 2012 at 14:09
  • Ohh... typo... fixed it! Commented Mar 7, 2012 at 14:17

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.