Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to create activity streams based on words or hashtags?


  • IdleWanderer
    Participant

    @idlewanderer

    Hi there,

    I’ve been trying to figure this one out by myself for a while now. I am new to BuddyPress, and not the best at coding, so in the end I couldn’t quite understand how to do exactly what I want.

    I am going to have multiple pages on wildlife species, and on every one of these pages I want an activty stream containing updates from members regarding that species only. So, if there’s a page for lion, then I want the activity stream to only include updates with the words “lion” “lions” and “panthera leo” (its latin name). The same will go for gray wolf, where I would include only “gray wolf” “gray wolves” and “canis lupus”. And so on for every species. How can I acutally make this happen? If it’s easier, I could make a stream for hashtags as well. Using #lion or #graywolf instead. Or maybe a combination of both words and hashtags.

    Thanks,
    Havard

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

  • Henry Wright
    Moderator

    @henrywright

    You will need to use bp_before_has_activities_parse_args for this. The article below has some really great examples to get you started:

    Using bp_parse_args() to filter BuddyPress template loops

    I’d imagine search_terms would be the most suitable parameter to filter in your case.


    IdleWanderer
    Participant

    @idlewanderer

    Thanks! This certainly helps, but I still can’t figure out exactly what code to input or where to put it to make this work.. I undestand which code to use, I am just not exactly sure how to write it up and where to insert the search terms and such.. And what conditionals would I put? I am sorry, I can get really confused when it comes to coding.. It’s always hard for me to wrap my head around these things..


    Henry Wright
    Moderator

    @henrywright

    You can add a custom function to the bp_after_has_activities_parse_args filter hook like this:

    add_filter( 'bp_after_has_activities_parse_args', function( $retval ) {
        // Change search-term to your own search term.
        $retval['search_terms'] = 'search-term';
        return $retval;
    } );

    IdleWanderer
    Participant

    @idlewanderer

    Thanks! I think I got it now!

    I just added this to my functions.php:

    function my_bp_activities_search_term_on_page_92( $retval ) {
        // Add search term for correct page
        if ( bp_is_page(92) ) {
            $retval['search_term'] = 'philippine tube-nosed fruit bat';
        }
     
        return $retval;
    }
    add_filter( 'bp_after_has_activities_parse_args', 'my_bp_activities_search_term_on_page_92' );

    Do you think this will work? I do not have any ways to test if it actually works yet.. Another thing, how do I add multiple search terms? Everytime I tried adding another one I got an error, I tried doing this: $retval['search_term'] = 'philippine tube-nosed fruit bat', 'philippine tube-nosed bat', 'nyctimene rabori';


    Henry Wright
    Moderator

    @henrywright

    Do you think this will work?

    I don’t think so. The param is actually search_terms, not search_term


    IdleWanderer
    Participant

    @idlewanderer

    Ok, I’ve fixed that now. Other than that it looks ok? I still can’t add multiple search terms the way I tried it though.. What is the correct way?


    IdleWanderer
    Participant

    @idlewanderer

    I just realized that “bp_is_page()” might not work as the page I am referring to is a normal wordpress page. Is this so? If so, would “is_page()” be correct?


    IdleWanderer
    Participant

    @idlewanderer

    I did manage to test it now, and it does work! I did have to change it from “bp_is_page()” to “is_page()”. Now it’s just figuring out how to add multiple search terms..


    Henry Wright
    Moderator

    @henrywright

    The search terms should be passed as a string just like you did in your code. For example:

    $retval['search_terms'] = 'philippine tube-nosed fruit bat';

    Also, is_page() should work. Try something like this:

    if ( is_page() ) {
        // Get the data you will use as a search term.
    
        // Filter search_terms
        $retval['search_terms'] = 'search terms here';
    }
    return $retval;

    IdleWanderer
    Participant

    @idlewanderer

    I just can’t seem to make it work..

    If I add this:
    $retval['search_terms'] = 'philippine tube-nosed fruit bat';

    I will only get result from people writing “philippine tube-nosed fruit bat”, but I also want to get results from people writing “nyctimene rabori”, as well (the latin name of the species). Or other variants of the name, such as plural versions and such. And I need the terms to be separated somehow. And that is what I can’t figure out..

    I need it to be something like this:
    $retval['search_terms'] = 'philippine tube-nosed fruit bat, nyctimene rabori';

    But then it won’t work.. As then only the enitre “philippine tube-nosed fruit bat, nyctimene rabori” will give results. And this one below just breaks my site:

    $retval['search_terms'] = 'philippine tube-nosed fruit bat', 'nyctimene rabori';

    I am getting really confused by this..


    Henry Wright
    Moderator

    @henrywright

    The search_terms parameter acts kind of like a search box (you can type in a term or phrase). To the best of my knowledge, just like a search box, it doesn’t handle multiple terms or phrases. For example philippine tube-nosed fruit bat will be treated as one phrase. You can’t specify 2 phrases such as philippine tube-nosed fruit bat || nyctimene rabori.


    Henry Wright
    Moderator

    @henrywright

    Because your query has become more complex than a single search term or phrase, I think you’ll need to use meta_query or filter_query instead.


    Henry Wright
    Moderator

    @henrywright

    So here’s my stab at the problem:

    $filter_query[] = array(
        'relation' => 'OR',
        array(
            'column'  => 'content',
            'value'   => 'philippine tube-nosed fruit bat',
            'compare' => 'LIKE'
        ),
        array(
            'column'  => 'content',
            'value'   => 'nyctimene rabori',
            'compare' => 'LIKE'
        )
    );
    $retval['filter_query'] = $filter_query;

    Please note I haven’t tested.


    IdleWanderer
    Participant

    @idlewanderer

    I see, I kinda figured as much. So, how would I go about using one of those? I did a search, but couldn’t find anything that directly relates to what I need, at least not that I know of. How would I use meta_queryto do what I want? Is it similar to what I just did? I guess I can’t just replace the words “search_terms” with “meta_query” and I will be all set?

    Typical, I finally figured out how one thing works, and then I have to figure out something new.. Hehe

    EDIT: I see you already posted an example. Thanks! I’ll try it now.


    IdleWanderer
    Participant

    @idlewanderer

    Okay, so what I tried now didn’t work, but that’s because I really do not know what I am doing. I’m just testing the waters with what I learned from the search terms and the code you just posted. I ended up having my functions.php file looking like this:

    $filter_query[92] = array(
        'relation' => 'OR',
        array(
            'column'  => 'content',
            'value'   => 'philippine tube-nosed fruit bat',
            'compare' => 'LIKE'
        ),
        array(
            'column'  => 'content',
            'value'   =>  'nyctimene rabori',
            'compare' => 'LIKE'
        )
    );
    
    function my_bp_activities_search_term_on_page_92( $retval ) {
        // Add search term for correct page
        if ( is_page(92) ) {
            $retval['filter_query'] = $filter_query[92];
        }
     
        return $retval;
    }
    add_filter( 'bp_after_has_activities_parse_args', 'my_bp_activities_search_term_on_page_92' );

    And that clearly did not work, so what will work?


    Henry Wright
    Moderator

    @henrywright

    Ah, that’s not quite how to use it. You should filter inside the function you’re hooking to bp_after_has_activities_parse_args. For example:

    add_filter( 'bp_after_has_activities_parse_args', function( $retval ) {
        $filter_query[] = array(
            'relation' => 'OR',
            array(
                'column'  => 'content',
                'value'   => 'philippine tube-nosed fruit bat',
                'compare' => 'LIKE'
            ),
            array(
                'column'  => 'content',
                'value'   => 'nyctimene rabori',
                'compare' => 'LIKE'
            )
        );
        $retval['filter_query'] = $filter_query;
        return $retval;
    } );

    IdleWanderer
    Participant

    @idlewanderer

    Yes! It works now! Thank you so very much! I guess I can add as many terms as I like now?


    Henry Wright
    Moderator

    @henrywright

    I guess I can add as many terms as I like now?

    We’ve specified OR as the relationship between the activity queries so you can add as many as you like. For example:

    array(
        'column'  => 'content',
        'value'   => 'philippine tube-nosed fruit bat',
        'compare' => 'LIKE'
    ),
    array(
        'column'  => 'content',
        'value'   => 'nyctimene rabori',
        'compare' => 'LIKE'
    ),
    array(
        'column'  => 'content',
        'value'   => 'something else',
        'compare' => 'LIKE'
    ),
    array(
        'column'  => 'content',
        'value'   => 'blah',
        'compare' => 'LIKE'
    )

    IdleWanderer
    Participant

    @idlewanderer

    Thanks! Great stuff! Is there any thing I can set instead of ‘LIKE’ if I want to exclude terms? It’s not that important, but if I could I’d try to avoid having posts about sea lions end up on the lion page.


    Henry Wright
    Moderator

    @henrywright

    Sure! Accepted operators are =, !=, >, >=, <, <=, IN, NOT IN, LIKE, NOT LIKE, BETWEEN, NOT BETWEEN, REGEXP, NOT REGEXP and RLIKE.

    So perhaps try NOT LIKE.


    IdleWanderer
    Participant

    @idlewanderer

    Thanks for those! NOT LIKE did not work. Adding it only made the activity stream show everything. If I removed the array with NOT LIKE it went back to only showing results with the word lion again.. I also tried != with the same result.

    Would it help to have something other than 'compare' in front of it maybe? If I add NOT LIKE it probably adds everything that does not compare with the word “sea lion”, which overrides the arrays with LIKE. At least that’s my theory.


    Henry Wright
    Moderator

    @henrywright

    @idlewanderer try specifying another fully-formed query. For example:

    array(
        'column'  => 'content',
        'value'   => 'philippine tube-nosed fruit bat',
        'compare' => 'LIKE',
        array(
            'column'  => 'content',
            'value'   => 'sea lions',
            'compare' => 'NOT LIKE'
        )
    ),
    array(
        'column'  => 'content',
        'value'   => 'nyctimene rabori',
        'compare' => 'LIKE',
        array(
            'column'  => 'content',
            'value'   => 'sea lions',
            'compare' => 'NOT LIKE'
        )
    )

    IdleWanderer
    Participant

    @idlewanderer

    I tried it, but it din’t work. At least it didn’t stop the filter rules already set up, like they did when I tried before.. So only words with lion shows up, but also sea lion.. Even with the code set up exactly like yours. Like this:

    function my_bp_activities_search_term_on_page_241( $retval ) {
        // Add search term for correct page
        if ( is_page(241) ) {
             $filter_query[] = array(
            'relation' => 'OR',
            array(
        'column'  => 'content',
        'value'   => 'lion',
        'compare' => 'LIKE',
        array(
            'column'  => 'content',
            'value'   => 'sea lion',
            'compare' => 'NOT LIKE'
        )
    ),
               array(
        'column'  => 'content',
        'value'   => 'lion',
        'compare' => 'LIKE',
        array(
            'column'  => 'content',
            'value'   => 'sea lion',
            'compare' => 'NOT LIKE'
        )
      ),
    );
        $retval['filter_query'] = $filter_query;
        }
     
        return $retval;
    }
    add_filter( 'bp_after_has_activities_parse_args', 'my_bp_activities_search_term_on_page_241' );

    Henry Wright
    Moderator

    @henrywright

    I don’t have enough data set up on my local install to test, but you could try this:

    add_filter( 'bp_after_has_activities_parse_args', function( $retval ) {
        $filter_query[] = array(
            'relation' => 'OR',
            array(
                'column'  => 'content',
                'value'   => 'philippine tube-nosed fruit bat',
                'compare' => 'LIKE'
            ),
            array(
                'column'  => 'content',
                'value'   => 'nyctimene rabori',
                'compare' => 'LIKE'
            )
        );
        $filter_query[] = array(
            array(
                'column'  => 'content',
                'value'   => 'sea lion',
                'compare' => 'NOT LIKE'
            )
        );
        $retval['filter_query'] = $filter_query;
        return $retval;
    } );

    IdleWanderer
    Participant

    @idlewanderer

    That solved it! Awesome! Thanks! 🙂

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