BuddyDev

Search

[Resolved] Get total elements count inside one gallery (by id)

  • Participant
    Level: Initiated
    Posts: 10
    Minoo on #4173

    Hello !

    There is a way to get the total of element inside 1 gallery ? I just have my Gallery ID.

    Thank you !

  • Keymaster
    (BuddyDev Team)
    Posts: 24708
    Brajesh Singh on #4174

    Hi Minoo,
    There are multiple ways to do it. Do you want to consider the visibility of media while showing the count or not?

    If we do not consider the visibility of media, the count will be very efficient.

  • Participant
    Level: Initiated
    Posts: 10
    Minoo on #4175

    Thank so much for your fast answer,

    It will be great if we can consider it, it will be strange if we annonce to the visitor 2 medias for one gallery and the gallery doesn’t display anything because the media are private !

    Thank you !

  • Keymaster
    (BuddyDev Team)
    Posts: 24708
    Brajesh Singh on #4176

    Hi,
    Here are the methods:-

    1. Without considering privacy mpp_gallery_get_media_count( $gallery_id ) will give the count of all media inside the gallery

    2. There is no builtin API for the count with privacy, so we will write a new function like this

    
    
    function mpp_custom_get_media_count( $gallery_id  ) {
    
    	$gallery = mpp_get_gallery( $gallery_id );
    
    	$args = array(
    		'gallery_id'    => $gallery->id,
    		'status'    => mpp_get_accessible_statuses( $gallery->component, $gallery->component_id ),
    		'fields'=> 'ID',
    		'per_page'  => -1,
    		'nopaging'=> true,
    	);
    
    	$gq = new MPP_Media_Query( $args );
    
    	$count = $gq->post_count;
    
    	 return $count;
    }
    
    

    and we can use it as we wish. Please do not overuse it, it is database intensive operation.
    Hope it helps.

  • Participant
    Level: Initiated
    Posts: 10
    Minoo on #4223

    Thank you Brajesh !

  • Keymaster
    (BuddyDev Team)
    Posts: 24708
    Brajesh Singh on #4233

    You are most welcome 🙂

The topic ‘ [Resolved] Get total elements count inside one gallery (by id)’ is closed to new replies.

This topic is: resolved