Skip to:
Content
Pages
Categories
Search
Top
Bottom

Buddypress activity filtering (search)


  • ARCangelGIRL
    Participant

    @arcangelgirl

    In my site to upload youtube videos in activity I’m using https://wordpress.org/plugins/buddypress-activity-plus/ I have modified the code, so the user could select video category. I have add 2 new columns in activity mysql table “video_category” and “video_subcategory”. That works just fine.

    The problem is that now I want to make activity feed searchable via categories and subcategories that I used.
    To filter activity feed I used this code in function.php

    if ($_POST['select_name'] === 'category_name') {
    	(bp_has_activities( bp_ajax_querystring( 'activity' ).'&search_terms=category_name' ) );
    	}

    It looks like that ( bp_ajax_querystring( ‘activity’ ).’&search_terms=category_name’ ) search only in mysql Activity table “content” column. But I want to search from my new columns “video_category” and “video_subcategory”. Because when i tried to add video category name in ‘content’ search is working just fine, but I want to search not in content column, but from my new ones.

    Any ideas how could I achieve that?

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

  • shanebp
    Moderator

    @shanebp

    I have add 2 new columns in activity mysql table “video_category” and “video_subcategory”.

    Adding new columns is not a good approach.

    Did you try adding your ‘categories’ as a type via
    https://codex.buddypress.org/developer/function-examples/bp_activity_add/ ?


    shanebp
    Moderator

    @shanebp

    Then you could search on those types like so:
    bp_has_activities( bp_ajax_querystring( 'activity' ) . '&action=video_category' )
    More info: https://codex.buddypress.org/developer/loops-reference/the-activity-stream-loop/#filtering-examples

    Hello @ARCangelGIRL, I’m facing the same situation and @shanebp solution worked for displaying the action I wanted in the activity stream. But the Load More button is loading all the actions, not just the one I created. I tried to filter the bp ajax querystring in many different ways but it didn’t work. The last try was this https://buddypress.org/support/topic/simply-trying-to-filter-the-bp_ajax_querystring-via-ajax/ .


    ARCangelGIRL
    Participant

    @arcangelgirl

    Thank you for your answer @shanebp !
    Now I’m little bit lost when should I use bp_activity_add function? The moment the user adds youtube URL and selects category the code should be something like this?

    if ($_POST['select_name'] === 'category_name') {
    	$activity_id = bp_activity_add( $args );
    	}

    This is the code I added at line 278 in /wp-content/plugins/buddypress-activity-plus/lib/class_bpfb_binder.php to create the action. The credit for this code is @Juanma in WordPress.org forum. (couldn’t find the original thread). In this case I’m creating an action for images upload.

    function ajax_update_activity_contents () {
      $bpfb_code = $activity = '';
      $aid = 0;
      $codec = new BpfbCodec;
      if (@$_POST['data']['bpfb_photos']) {
        $images = $this->move_images($_POST['data']['bpfb_photos']);
        $bpfb_code = $codec->create_images_tag($images);
        $type = 'bpfb_share_wall_photos';
      }
    
      $bpfb_code = apply_filters('bpfb_code_before_save', $bpfb_code);
    
      // All done creating tags. Now, save the code
      $gid = (int)@$_POST['group_id'];
      if ($bpfb_code) {
        global $bp;
        $content = @$_POST['content'] . "\n" . $bpfb_code;
        $content = apply_filters('bp_activity_post_update_content', $content);
        $user_id = $bp->loggedin_user->id;
        $userlink = bp_core_get_userlink( $user_id );
    
        $author = bp_core_get_userlink( $author_id );
        $activity_url = bp_activity_get_permalink( $item_id );
    
        $aid = $gid ?
          groups_post_update(array('conte nt' => $content, 'group_id' => $gid)) : 
          bp_activity_add(
            array(
              'action' => apply_filters( 'activity_update', sprintf( __( '%s shared a link', 'buddypress' ), $userlink ), $user_id ),
              'content' => $content,
              'component' => 'activity',
              'type' => $type,
              'user_id' => $user_id
            )
          );
          if ($aid) {
    			ob_start();
    			if ( bp_has_activities ( 'include=' . $aid ) ) {
    				while ( bp_activities() ) {
    					bp_the_activity();
    					if (function_exists('bp_locate_template')) bp_locate_template( array( 'activity/entry.php' ), true );
    					else locate_template( array( 'activity/entry.php' ), true );
    				}
    			}
    			$activity = ob_get_clean();
    		}
    		header('Content-type: application/json');
    		echo json_encode(array(
    			'code' => $bpfb_code,
    			'id' => $aid,
    			'activity' => $activity,
    		));
    		exit();
    	}
    }

    And this is the code that works for activity stream. It shows just the activities with this action, but when you click the ‘load more’ button it loads the regular stream.
    <?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . '&action=bpfb_share_wall_photos') ) : ?>


    ARCangelGIRL
    Participant

    @arcangelgirl

    @brgweb did you try to change the number of how many activities you could show ‘per_page’ to a very big number? Then ‘Load more’ button should not appear if the number is not reached.

    @ARCangeGIRL actually that’s my Plan B right now, until I find the solution to filter the ajax call (without hardcode)…

    I just removed the load more icon and set the stream for ‘latest images’.

    Thank you for your idea! Could you manage to make it work in search (with load more)?


    ARCangelGIRL
    Participant

    @arcangelgirl

    @brgweb thank you for your help! I tried your code, but I think it’s not what I need. Code adds a filter, but in my case when uploading video the user should choose one of the 12 categories and each of that category has ~20-30 subcategories… It a select dropdown to choose category at first and then subcategory.

    So at the end of the day I need about 200-300 filters. Is it possible to do that with the snippet you send me?

    @ARCangelGIRL I think you could make the $type receive the category + subcategory as an unique action. I don’t know how you’re passing the category/subcategory trhough POST, but could try something like:

    function ajax_update_activity_contents () {
      $bpfb_code = $activity = '';
      $aid = 0;
      $cat = $_POST['category'];
      $subcat = $_POST['subcategory']
      $codec = new BpfbCodec;
      if (@$_POST['data']['bpfb_photos']) {
        $images = $this->move_images($_POST['data']['bpfb_photos']);
        $bpfb_code = $codec->create_images_tag($images);
        $type =  $cat . $subcat;
      }
    ...
    

    Then in search you could make the dropdown select again for the category and subcategory and use the code:
    <?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . '&action=' . $category . $subcategory) ) : ?>

    But for me this is not working with ajax though…


    ARCangelGIRL
    Participant

    @arcangelgirl

    @brgweb In plugins/buddypress-activity-plus/js/bpfb_interface.js in line ~49-50 I’m passing categories drop down form

    $('.bpfb_preview_container').empty().html(data);
    $('.bpfb_action_container').html('  <div id="category"> <select name="video_category"><option value="category1">Category1</option>........ 

    In line ~435

    $(document).on('click', '#bpfb_submit', function () {
    		var params = _bpfbActiveHandler.get();
    		var group_id = $('#whats-new-post-in').length ? $('#whats-new-post-in').val() : 0;
    		var category = $('#category').val();
    		var subcategory = $('#subcategory').val();
    		$.post(ajaxurl, {
    			"action": "bpfb_update_activity_contents",
    			"data": params,
    			"content": $text.val(),
    			"group_id": group_id,
    			"category": category,
    			"subcategory": subcategory
    

    And then in plugins/buddypress-activity-plus/lib/class_bpfb_binder.php in line ~328

    $query='UPDATE wp_bp_activity SET video_category="'. $_POST['category'].'", video_subcategory="'. $_POST['subcategory'].'" WHERE id=' . $aid; 
    		$dummy=mysql_query($query);

    I’ll try with the code you provide to see if it’s working.


    ARCangelGIRL
    Participant

    @arcangelgirl

    @brgweb I tried the code you send me, but that’s what I got.
    When I use this code, then in database in type column words ‘video_library_category’ is saved.

    function ajax_update_activity_contents () {
    		$bpfb_code = $activity = '';
    		$aid = 0;
    		$video_library_category = $_POST['industries_subcategories'];
    		$codec = new BpfbCodec;
    		if (@$_POST['data']['bpfb_video_url']) {
    			$bpfb_code = $codec->create_video_tag($_POST['data']['bpfb_video_url']);
    			$type = 'video_library_category';

    But when I use this code $type = $video_library_category than video upload is not working at all. I paste url, press preview, save, page reloads and nothing happens, nothing is saved. Any ideas with this how can I save subcategorie?

    function ajax_update_activity_contents () {
    		$bpfb_code = $activity = '';
    		$aid = 0;
    		$video_library_category = $_POST['industries_subcategories'];
    		$codec = new BpfbCodec;
    		if (@$_POST['data']['bpfb_video_url']) {
    			$bpfb_code = $codec->create_video_tag($_POST['data']['bpfb_video_url']);
    			$type = $video_library_category;

    Try to echo or var_dump $video_library_category to check if it is set

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Buddypress activity filtering (search)’ is closed to new replies.
Skip to toolbar