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;
}
}
pre_get_posts
will affect all queries, you need to check context to apply it to the specific query you want to target.