Best way to fix bad count on All | Mine | Published

I tweaked the default behavior of the post listing page (for a custom post type) via pre_get_posts, the thing is that now the count of All | Mine | Published isn't the right one.

I'm aware there is this filter: views_edit-{post_type}

But we are editing the HTML output, so to correct this value i'm currently doing :

$views['all'] = preg_replace(/\(\d+\)/, ($new_count), $views['all']);

Which I find it dirty, I'm aware of the filter wp_count_posts which return this:

stdClass Object
(
    [publish] = 38
    [future] = 0
    [draft] = 0
    [pending] = 0
    [private] = 0
    [trash] = 0
    [auto-draft] = 0
    [inherit] = 0
    [request-pending] = 0
    [request-confirmed] = 0
    [request-failed] = 0
    [request-completed] = 0
    [acf-disabled] = 0
)

But All and Mine are missing, so I can't change those with this filter, there is a better way of doing it ?

EDIT The tweak:

function custom_filter_devis_list(WP_Query $query) {
   if ($query-get('post_type') !== 'devis') return;
   if (current_user_can('administrator')) return;
   if ($query-is_main_query()  is_admin()) {
      $query-set('meta_key', 'partenaire');
      $query-set('meta_value', get_current_user_id());
   }
}
add_action('pre_get_posts', 'custom_filter_devis_list');

But as far as I'm aware, this is supposed to mess up the count process when altering the listing with pre_get_posts, I found stackoverflow post fixing it by using views_edit-{post_type}, but to me it seems like a dirty fix

Topic views filters hooks posts custom-post-types Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.