2

I'm missing a basic concept. I'm trying to add several "subsubsub" links or quick links to

"All | published | draft | trash".

Trying to add "Returned Custom Post Type (#)" and a few others. The (#) is what keeps getting changed to 0 unless you're on the page with the posts.

I just tried setting posts_per_page to the query as a comment has said here, but no change.

I have them showing up, but the count is wrong. They all show 0 unless you click on the link, then it returns the proper count. If I go to lets say "Returned Custom Post Type", the "Used Custom Post Type" goes to 0.

I've tried wp_reset_query() and wp_reset_post_data(); after each query, but no luck.

I also have a function for pre_get_posts, which just adds $query->set('post_type') to filter what's on the page, but I would assume the links are separate from pre_get_posts.

Anyone point me in the right direction to what I'm doing wrong?

Oh and the reason it's add_filter('blah',array($THIS,'etc') ) is because it's within a class so I have to specify "$this".

add_filter('views_edit-custom_post_type',array($this,'add_custom_post_type_to_subsubsub'));

public function add_custom_post_type_to_subsubsub($views){
    if( ( is_admin() ) && ( $_GET['post_type'] == 'custom_post_type' ) ) {

         $query = array(
             'post_type'   => 'custom_post_type',
             'post_status' => 'publish',
             'meta_key'    => 'special_meta_key'
         );

         $result = new WP_Query($query);
         $class = ($_GET['special_meta_key'] == '1' && $_GET['special_meta_key_returned'] != '1') ? ' class="current"' : '';

         $views['custom_post_type'] = sprintf(__('<a href="%s" '.$class.'>custom_post_type Vouchers <span class="count">(%d)</span></a>', 'custom_post_type' ), admin_url('edit.php?post_type=custom_post_type&special_meta_key=1'), $result->found_posts);

         $querytwo = array(
             'post_type'   => 'custom_post_type',
             'post_status' => 'publish',
             'meta_key'    => 'special_meta_key',
             'meta_query' => array(
                    array(
                        'key' => 'usage_count',
                        'value' => '0',
                        'compare' => '>'
                    )
                )
         );

         $resulttwo = new WP_Query($querytwo);
         $class2 = (isset($_GET['special_meta_key_returned']) && $_GET['special_meta_key_returned'] == "1") ? ' class="current"' : '';

         $views['custom_post_type_returned'] = sprintf(__('<a href="%s" '.$class2.'>Returned custom_post_type Vouchers <span class="count">(%d)</span></a>', 'custom_post_type' ), admin_url('edit.php?post_type=custom_post_type&special_meta_key_returned=1'), $resulttwo->found_posts);

        $querythree = array(
             'post_type'   => 'custom_post_type',
             'post_status' => 'publish',
             'meta_query' => array(
                    array(
                        'key' => 'usage_count',
                        'value' => '0',
                        'compare' => '>'
                    )
                )
         );

         $resultthree = new WP_Query($querythree);
         $class3 = ($_GET['used_type'] == '1') ? ' class="current"' : '';

         $views['used_types'] = sprintf(__('<a href="%s" '.$class3.'>Used Vouchers <span class="count">(%d)</span></a>', 'custom_post_type' ), admin_url('edit.php?post_type=custom_post_type&used_type=1'), $resultthree->found_posts);

         //exit(var_dump($resulttwo));
         return $views;
     }

}
2
  • 1
    pre_get_posts will affect all queries, you need to check context to apply it to the specific query you want to target.
    – Milo
    Aug 22, 2018 at 15:52
  • Thanks, that led me down the right path. Just needed a is_main_query() to the pre_get_posts
    – Darius
    Aug 22, 2018 at 16:40

1 Answer 1

2

Use pre_get_posts filter & use is_main_query() function to check for the context (as you've figured out in the conversation with @Milo), however, don't use is_main_query() directly.

When you use is_main_query() to check the context, it always checks against the main global $wp_query, instead of another custom query object you may want. So it may cause unexpected behaviour.

To make sure you are filtering the correct context, use the $query parameter that's passed to the callback function of pre_get_posts filter hook. So instead of checking is_main_query(), you'll be checking $query->is_main_query(). For example:

function wpse_example_filter( $query ) {
    if ( is_admin() && $query->is_main_query() ) {
        // $query modification CODE
    }
}
add_action( 'pre_get_posts', 'wpse_example_filter' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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