Kindly try adding the following code to the bottom of your theme’s functions.php.
function aiovg_filter_videos_count( $terms, $taxonomies ) {
if ( is_admin() ) {
return $terms;
}
if ( ! in_array( 'aiovg_categories', $taxonomies ) ) {
return $terms;
}
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$args = array(
'post_type' => 'aiovg_videos',
'posts_per_page' => -1,
'post_status' => array( 'private', 'publish' ),
'fields' =>'ids',
'no_found_rows' => true,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'tax_query' => array(
array(
'taxonomy' => 'aiovg_categories',
'field' => 'term_id',
'terms'=> $term->term_id
)
)
);
$aiovg_query = new WP_Query( $args );
if ( $aiovg_query->have_posts() ) {
$term->count = count( $aiovg_query->posts );
}
}
}
return $terms;
}
add_filter( 'get_terms', 'aiovg_filter_videos_count', 10, 2 );
Hope this solved your issue!
Thank you, that worked by showing the video count. I still can’t activate “hide categories with no videos”, as this still makes categories with only private videos disappear from category view. That is OK for my purposes as I will be populating those categories within the coming weeks. However if you’d like me to test anything out regarding the issue, let me know.
Kindly replace our previous code with the following,
function aiovg_filter_videos_count( $terms, $taxonomies, $args ) {
if ( is_admin() ) {
return $terms;
}
if ( ! in_array( 'aiovg_categories', $taxonomies ) ) {
return $terms;
}
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$hide_empty = ! empty( $args['custom_hide_empty'] ) ? 1 : 0;
foreach ( $terms as $index => $term ) {
$query_args = array(
'post_type' => 'aiovg_videos',
'posts_per_page' => -1,
'post_status' => array( 'private', 'publish' ),
'fields' =>'ids',
'no_found_rows' => true,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'tax_query' => array(
array(
'taxonomy' => 'aiovg_categories',
'field' => 'term_id',
'terms'=> $term->term_id
)
)
);
$aiovg_query = new WP_Query( $query_args );
if ( $aiovg_query->have_posts() ) {
$term->count = count( $aiovg_query->posts );
} else {
if ( $hide_empty ) {
unset( $terms[ $index ] );
}
}
}
}
return $terms;
}
add_filter( 'get_terms', 'aiovg_filter_videos_count', 10, 3 );
function aiovg_custom_categories_args( $args ) {
if ( isset( $args['hide_empty'] ) ) {
$args['custom_hide_empty'] = $args['hide_empty'];
$args['hide_empty'] = 0;
}
return $args;
}
add_filter( 'aiovg_categories_args', 'aiovg_custom_categories_args' );
Hope this solved your issue!