Skip to:
Content
Pages
Categories
Search
Top
Bottom

Is filtering Activity on CPT meta info possible?


  • Ben Riga
    Participant

    @benbuzztoniccom

    I’m trying to build a page to show a filtered activity stream but am completely unsure how to approach the problem. I’ve built my solution using CPTs and would like to be able to query based on custom meta box info in the CPTs.

    For example, my CPTs include Beer and Check-In. So a user could “check-in” and mention which Beer they are drinking. The check-ins are recorded as activities and these display just fine in the activity stream (“Ben is drinking Molson Export Ale and rated it a 5”). Now I’d like to build a page that shows information about a Beer and the list of check-in activities for that particular Beer.

    I think I know how to query the CPTs to get the info I want but don’t want to have to duplicate the BP functions. Ideally I’d like to filter the activity stream based on the info from the CPT custom meta info but I haven’t found a way to do that.

    Have any of you seen something like that? Or could you provide an educated guess of where I might look for more info?

    Any help or support would be greatly appreciated as I am stumped.

    Thanks,
    Ben

Viewing 5 replies - 1 through 5 (of 5 total)

  • Ben Riga
    Participant

    @benbuzztoniccom

    Would anyone have any guidance on how to tackle a problem like this? Any advice at all would be greatly appreciated.

    Thanks,
    Ben


    Ben Riga
    Participant

    @benbuzztoniccom

    I am struggling with this one so any nudge in the right direction (however small) would be great.

    I have tried going at it from the other direction but that seems like it will duplicate a lot of BuddyPress code and so doesn’t feel right. By this I mean, if I can’t query the BuddyPress activities based on the meta from the cpt the activities come from, I figured I could try querying the original CPT and filtering on the meta (easily done) and then outputting those as if they are activities. I’ve gotten quite far down that path by duplicating a lot of the code from activity\entry.php. But I’m missing a lot still calculating the date, adding comments, favoriting/reactions etc etc. It doesn’t feel right though and I’m sure I’ll miss things so that the two will never be the same and I’ll be stuck maintaining something that I prob should never have written.

    One thought I had was that it might be possible to pull the cpt and find the associated activity id (like secondary_item_id in activity but in the other direction). I have not found a link between the two in that direction.

    Then I thought, well if that link from cpt-to-activity is not available by default maybe I could grab it at the time of the initial creation of the activity and save it. Is that possible?

    Apologies if this is starting to sound like a stream of consciousness. I’m complete lost right now.

    Thanks for any help and support.

    Regards,
    Ben


    danbp
    Moderator

    @danbp

    Hi,

    it’s not the solution but an example you can perhaps use.
    The snippet handles the object members and sort them by type “contributing”
    Replace object by activity and type by “beer” and see what you can do from that.

    // add order options to members loop
    function ch_member_order_options() {
    ?>
       <option value="contributing"><?php _e( 'Contributing Members', 'buddypress' ); ?></option>
    <?php
    }
    add_action( 'bp_members_directory_order_options', 'ch_member_order_options' );
    
    // filter ajax members by contributing
    function ch_filter_ajax_querystring( $querystring = '', $object = '' ) {
    
    	if( $object != 'members' )
    		return $querystring;
    
    	$defaults = array(
    		'type'            => 'active',
    		'action'          => 'active',
    		'scope'           => 'all',
    		'page'            => 1,
    		'user_id'         => 0,
    		'search_terms'    => '',
    		'exclude'         => false,
    	);
    
    	$ch_querystring = wp_parse_args( $querystring, $defaults );
    
    	if( $ch_querystring['type'] == 'contributing' ) {
    	
           // grab members by Member Type role declared on xprofile	
    	$users = get_users( array( 'fields' => array( 'ID' ), 'role' => 'contributor' ) );
    		
    		$users_str = '';
    		foreach ( $users as $user ) {
    				 $users_str .=  $user->ID . ',';
    			}
    		$users_str = rtrim($users_str, ",");
    
    		$ch_querystring['include'] = $users_str; 
    		$ch_querystring['type'] = 'alphabetical'; 
    		
    		return $ch_querystring;
    		
    	}	
    	
    	else
    		return $querystring;
    			
    }
    add_filter( 'bp_ajax_querystring', 'ch_filter_ajax_querystring', 20, 2 );

    https://buddypress.org/support/topic/adding-new-order-by-on-members-loop/

    Members Loop

    Member Types


    Ben Riga
    Participant

    @benbuzztoniccom

    Thanks, Dan,

    I’ll dig in and try that out. There’s a lot here I don’t understand so may take some time. Thanks for the tip. I’ll let you know how I make out.

    Regards,
    Ben


    Ben Riga
    Participant

    @benbuzztoniccom

    Dan,

    I spent some time on this and have figured out the loop that will give me the activity entries I need. I’m trying to add the list of activities on the same page I display the Beer custom post type. So show the beer info at the top and the list of related activities below it. I’m doing that on a theme page and would need to call the activity/entry.php for each of the entries. Is there a way to do that or would I need to copy the code from there?

    The query will look something like this:

    $args = array(
    	'post_type' => 'check_in',
    	'posts_per_page' => '-1',
    	'post_status' => array(
    		'publish',
    	),
    	'meta_key' => 'check_in_details_beer-id',
    	'meta_value' => $current_post
    
    );
    
    // run the query
    $query_checkins = new WP_query ( $args );
    
    // check the query returns posts
    if ( $query_checkins->have_posts() ) :
    
    	while ( $query_checkins->have_posts() ) : $query_checkins->the_post();
    
    		$ci_postid = get_the_ID(); 
    
    		if ( bp_has_activities( bp_ajax_querystring( 'activity' )  . '&secondary_id='. $ci_postid ) ) : 
    

    Thanks for the help and support,
    Ben

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar