• Resolved mglasco

    (@mglasco)


    I have my page setup to make some videos only accessible to logged-in users. To do that I use a user-role editor, and then make the video private so that only logged in-users can view the content. That works well, but content count doesn’t consider private videos. I also can’t turn on “Hide empty categories” because it sees those categories as empty, even if logged-in. Let me know if you have a workaround, but no rush.

    • This topic was modified 2 years, 9 months ago by mglasco.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor wpvideogallery

    (@wpvideogallery)

    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!

    Thread Starter mglasco

    (@mglasco)

    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.

    Plugin Contributor wpvideogallery

    (@wpvideogallery)

    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!

    Thread Starter mglasco

    (@mglasco)

    That worked Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Include Private Videos in Count?’ is closed to new replies.