Skip to:
Content
Pages
Categories
Search
Top
Bottom

Order/sort activity by “most favorites”


  • celsosoares
    Participant

    @celsosoares

    Hi, I would like to create a button on the activity stream page that once clicked sorts the activities by the number of favorites each of them have. In other words, the point is to show the “most popular” activity items, based on how much each was “favorited”.
    Note: The sorting is for favorites by everyone, no just the user clicking the button, for each activity item, and should also return the number of favorites.

    Any help would be greatly appreciated as I can’t find anything on google or anywhere :(

    Many thanks!

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

  • celsosoares
    Participant

    @celsosoares

    Anyone?


    r-a-y
    Keymaster

    @r-a-y

    This isn’t as straightforward as it seems.

    If you know your way around WordPress, here’s one way of doing what you want to achieve:

    The favorite count for each activity item is stored in the activity meta table (wp_bp_activity_meta). So you could probably query the DB and grab all the “activity_id” sorted descendingly by the “favorite_count” meta_value.

    Then you could use a BP activity loop and use the `include` parameter to add the activity IDs you grabbed from your WPDB query.


    celsosoares
    Participant

    @celsosoares

    @r-a-y thanks for your reply! I don’t know a lot of php so please bear with me. This is what I did, based on what you suggested:

    `
    get_results(“SELECT activity_id FROM $wpdb->wp_bp_activity_meta WHERE meta_value = ‘favorite_count’ ORDER BY meta_value”);
    foreach ($favorite_id as $favorite_id) {
    echo $favorite_id->activity_id;
    }
    ?>

    ` etc..

    Nothing gets echoed in the foreach loop, but $favorite_id echoed on its own returns “array”. I’m sure I’m doing something wrong as what gets outputted in the browser is “Sorry, there was no activity found. Please try a different filter.” even though there are 4 activity items favorited by two different test users. :(

    What am I doing wrong? Many thanks for any help!!


    r-a-y
    Keymaster

    @r-a-y

    @celsosoares – I haven’t debugged your code fully, but this section:

    `WHERE meta_value = ‘favorite_count’ ORDER BY meta_value`

    should be:

    `WHERE meta_key = ‘favorite_count’ ORDER BY meta_value DESC`

    Also, the documentation for the `include` parameter indicates that you have to pass a comma-delimited list of activity IDs.

    So you’ll need to change this:

    to:

    `
    <?php
    $ids = implode( ‘,’, $favorite_id );

    if ( bp_has_activities( ‘include=’ . $ids ) ) : ?>`

    Again, I haven’t tested this, but give it a shot!

    I’ve been looking to do this as well. But bp_activity_get_specific automatically applies a DESC order as a default. Essentially failing to maintain the ordering of ids

    I can only see this working the way we want is through a custom loop or functions that bypass this.

    BP 1.28, I’m unsure of any changes to this for 1.3


    celsosoares
    Participant

    @celsosoares

    @r-a-y , @Dankicity thank you for your replies. Yeah I can’t get this to work either. Following r-a-y’s steps all I get returned is ALL of the activity items, sorted chronologically. That’s likely because my code sucks though :) If anyone gets this to work, it would be awesome! It’s the only thing left to sort out on this app I’m making :(


    imjscn
    Participant

    @imjscn

    BuddyPress Like plugin might provide similiar feature.
    Last year, it announced V0.9 to include statics in Dec., but the developer has been missing since then.


    celsosoares
    Participant

    @celsosoares

    Thanks @imjscn , but I get the same result using the Like plugin. It uses the same table so the only value I changed was the meta_value, to “liked_count”. I get ALL the activity items returned, ordered chronologically, like before.

    Anyone out there want to give it a shot and post the code here? Or write a plugin :)

    `<?php
    $popular_activities = my_get_popular(); // returns an array of activity IDs
    for ($i = 0; $i<count($popular_activities); $i++){

    if (empty($popular_activities[$i])) continue;

    $activity = new BP_Activity_Activity($popular_activities[$i]);

    if (!$activity) continue; ?>

    // your customized activity entry template

    `

    It’s a pretty ugly way of doing it but it got me started. Main defect, as I don’t yet get inserting activities into the activity template (without it doing it’s own query), is that I do one query to get the popular ids and then a query per activity (done in “BP_Activity_Activity”). Also, you’re bypassing all the the filters, pagination and other stuff in the activities_template, or I am. Since I’m just directly printing the values…

    `
    content; ?>
    user_id ); ?>
    user_id ); ?>
    date_recorded ); ?>
    $activity->user_id, ‘width’ => 20, ‘height’ => 20, ’email’ => $activity->user_email) ); ?>
    `

    You would need to do a query … [edit] nevermind ray gave that already

    `
    function popularactivities() {

    $popular_activities = my_get_popular();
    foreach ($popular_activities as $favorite_id) {
    $activity_id .= $favorite_id->activity_id;
    }
    return $activity_id;
    }

    $favorite_id = popularactivities();

    //custom code here…

    `

    p.s I’ve not tested this it’s just some logic workaround from all the codes above .

    You missed the point of avoiding bp_has_activities, in that it automatically reorders the includes through it’s own query.

    i’ll check this one tonight ,I’m at work at this moment. :)

    You’d need to (this is most undoubtedly wrong)
    `
    ‘SELECT wp_bp_activity.id as id
    FROM wp_bp_activity
    LEFT JOIN wp_bp_activity_meta
    ON wp_bp_activity.id = wp_bp_activity_meta.activity_id
    WHERE wp_bp_activity_meta.meta_key = “favorite_count”
    GROUP BY wp_bp_activity.id
    HAVING COUNT (wp_bp_activity_meta.activity_id) > 0
    ORDER BY COUNT (wp_bp_activity.id) DESC
    LIMIT 20′
    `
    The reason I merged these is that you should be able to SELECT * activity and set them all as the $activity_template->activities and use the activity_template loop (bp_has_activities) but my attempts so far have failed.

    that seems to be right.
    if you want to order it by most favorites then you have to order it by COUNT.


    juicenelson
    Participant

    @juicenelson

    Did any of you guy ever get this to work, I’m trying to accomplish the same thing.


    Chef
    Participant

    @splendidangst

    bump


    mathieu.urstein
    Participant

    @mojomateo

    Someone have been able to do it?


    Chef
    Participant

    @splendidangst

    Not that I have found

Viewing 18 replies - 1 through 18 (of 18 total)
  • The topic ‘Order/sort activity by “most favorites”’ is closed to new replies.
Skip to toolbar