Skip to:
Content

BuddyPress.org

Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#6629 closed enhancement (wontfix)

Accept array for the parameter item_id for bp_activity_add() function

Reported by: tafmakura's profile Tafmakura Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Core Keywords:
Cc:

Description

For a project I am working on I want my custom activity to have dynamic content of the post that invokes the activity when it is created instead of the static data that is passed to the activity's 'content' parameter, I achieved this by passing the post id as the secondary_item_id when the activity is created and then creating a custom entry.php template that displays activity content based on the secondary item_id (i.e featured image and exerpt) this works well. The problem is i now have activities that are suppose to show multiple posts in the activity content (don't ask how or why). Using the method above I can simply save the post id's into the secondary_item_id parameter as an array or comma separated string and then later display them by executing a loop.

The problem is that bp_activity_add() only accepts numbers(no spaces or letters ) and it will not accept an array either. I am no PHP developer but can secondary_item_id not accept an array or comma string or perhaps should there not be another parameter where this can be stored. i ended up using the content parameter to store a comma separated list of post IDs. It is untidy because by default the activity function will display this info yet I want it hidden.

Change History (4)

#1 @boonebgorges
9 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Hi Tafmakura - Thanks for the ticket request.

The reason why item_id and secondary_item_id accept only integers is that the wp_bp_activity database table only accepts integer values in this column. This fact about the database schema is part of what allows the activity component to scale as well as it does. So I'm afraid that it's not something we can change directly.

For your purposes, I'd consider using activity metadata instead of secondary_item_id. It'd look something like this:

// When creating the activity item:
$activity_id = bp_activity_add( $args );

bp_activity_add_meta( $activity_id, 'wp_post_id', $post_1 );
bp_activity_add_meta( $activity_id, 'wp_post_id', $post_2 );
// etc

// Then, when rendering the dynamic content:
$wp_post_ids = bp_activity_get_meta( $activity_id, 'wp_post_id', false );
foreach ( $wp_post_ids as $wp_post_id ) {
    // get the content of $wp_post_id
}

If you ever need to query activity items that match a specific post ID, use 'meta_query':

$activities = bp_activity_get( array(
    // ...
    'meta_query' => array(
        array(
            'key' => 'wp_post_id',
            'value' => $wp_post_id,
        ),
    ),
    // ...
) );

This way you can do it without resorting to ugly stuff like comma-separated lists :)

#2 @Tafmakura
9 years ago

Thanks boone It worked so well it brought tears to my eyes hahaha. I ended up storing the ids as a array I hope that has no disadvantages when calling the data later, I felt it would be more efficient.

Last edited 9 years ago by Tafmakura (previous) (diff)

#3 @boonebgorges
9 years ago

Glad to hear it worked!

Storing as an array is fine. The only potential limitation is if you later want to make the following query: "show me all activity items associated with WP post 123" (like I showed in my 'meta_query' example). This won't work if each post doesn't have its own row in the activitymeta table.

#4 @DJPaul
8 years ago

  • Component changed from API to Core
Note: See TracTickets for help on using tickets.