Get Post ID from Notification
-
How do I get the Post ID from an activity notification?
-
The goal being … I want to make a clickable link in the notification to go to the post corresponding to the notification.
Or how can I add a column in the database table bp_notifications for post_id?
Thanks!
I have tried about 100 different things and I thought this would work:
get_post_meta($post->ID,'thread_id');
You can only get the post ID if it was added at the point
bp_notifications_add_notification()
was used. How are you adding the notification?We hooked into a liking plugin and wp-comments to include notifications for activity on those two actions.
Then the notifications are coming in Ajax to a flyout window. 😀
Can I simply add post_id into this?
function bp_notifications_add_notification( $args = array() ) { $r = wp_parse_args( $args, array( 'user_id' => 0, 'item_id' => 0, 'secondary_item_id' => 0, 'component_name' => '', 'component_action' => '', 'date_notified' => bp_core_current_time(), 'is_new' => 1, 'allow_duplicate' => false, ) ); // Check for existing duplicate notifications if ( ! $r['allow_duplicate'] ) { // date_notified, allow_duplicate don't count toward // duplicate status $existing = BP_Notifications_Notification::get( array( 'user_id' => $r['user_id'], 'item_id' => $r['item_id'], 'secondary_item_id' => $r['secondary_item_id'], 'component_name' => $r['component_name'], 'component_action' => $r['component_action'], 'is_new' => $r['is_new'], ) ); if ( ! empty( $existing ) ) { return false; } } // Setup the new notification $notification = new BP_Notifications_Notification; $notification->user_id = $r['user_id']; $notification->item_id = $r['item_id']; $notification->secondary_item_id = $r['secondary_item_id']; $notification->component_name = $r['component_name']; $notification->component_action = $r['component_action']; $notification->date_notified = $r['date_notified']; $notification->is_new = $r['is_new']; // Save the new notification return $notification->save(); }
Didn’t mean to post this.
The post ID should be passed to that function as either
item_id
orsecondary_item_id
. Then, if you have the notification ID, you can retrieve the data (post ID) at a later date withbp_notifications_get_notification()
Wicked awesome! Going to check it out now.
$notificationpostid = bp_notifications_get_notification(secondary_item_id);
Just to note,
bp_notifications_get_notification()
returns anBP_Notifications_Notification
object so to getitem_id
you’d just access it like this:$obj = bp_notifications_get_notification( $id ); echo $obj->item_id;
Is this the proper way to have it setup at the function?
function bp_notifications_add_notification( $args = array() ) { $r = wp_parse_args( $args, array( 'user_id' => 0, 'item_id' => $post_id, 'secondary_item_id' => ,
I also don’t know what to do with:
$obj = bp_notifications_get_notification( $id ); echo $obj->item_id;
Do I make a function of it?
This is my code:
<?php while ( bp_the_notifications() ) : bp_the_notification(); $user_id = $bp->notifications->query_loop->notification->secondary_item_id; //bp_notifications_mark_notification(bp_get_the_notification_id(),false); ?>
<?php $messageId = buddypress()->notifications->query_loop->notification->item_id; $message = messages_get_message($messageId); $image = null; if ($message) { $title = apply_filters( 'bp_get_message_thread_from', bp_core_get_userlink( $message->sender_id ) ); if ($message->subject == "comment on your post") { $title .= " commented on your post"; } elseif ($message->subject == "like your post") { $title .= " liked your post"; } else { $title = null; } $image = bp_messages_get_meta($messageId, 'images'); $obj = bp_notifications_get_notification( $id ); } ?>
And the echo:
<div><a href="/pins/<?php echo $obj->item_id; ?>">VIEW</a></div>
function allofdalights(){ $obj = bp_notifications_get_notification( $id ); echo $obj->item_id; }
Thanks from the team at Uniprint QLD. We managed to fix our code after reading this thread.
Great job guys – all working again : )
What’d you do to fix your code? I’m still stuck! 😀
You’ll need to replace
$id
in my snippet with the actual ID of the notification. You should be able to get that withbp_get_the_notification_id()
if you’re using my snippet within the loop.Ack my bad! I’m fairly new to php and super new to Buddypress.
Ok soooo … in the file notifications-loop.php inside the loop I’ve added:
$notificationid7 = bp_get_the_notification_id(); $obj = bp_notifications_get_notification( $notificationid7 );
And where I need to display the link (also in notifications-loop.php):
<a href="/pins/<?php echo $obj->item_id; ?>">view</a>
Lastly, where I passed the item ID in bp-notifications-functions.php:
function bp_notifications_add_notification( $args = array() ) { $r = wp_parse_args( $args, array( 'user_id' => 0, 'item_id' => $post_id, 'secondary_item_id' => 0,
However it’s echoing the notification number … not the post ID.
You shouldn’t need to do anything to bp-notifications-functions.php so revert those changes an your code should be good to go.
After reverting the file … it’s still just displaying the “notification id.”
Inside the notifications loop can you do this and paste what you get?
$obj = bp_notifications_get_notification( bp_get_the_notification_id ); var_dump( $obj );
Is this what you were looking for?
object(BP_Notifications_Notification)#394 (8) { ["id"]=> string(26) "bp_get_the_notification_id" ["item_id"]=> NULL ["secondary_item_id"]=> NULL ["user_id"]=> NULL ["component_name"]=> NULL ["component_action"]=> NULL ["date_notified"]=> NULL ["is_new"]=> NULL }
My apologies, I missed parentheses. It should be:
$obj = bp_notifications_get_notification( bp_get_the_notification_id() ); var_dump( $obj );
Can you paste what you get with that updated snippet?
object(BP_Notifications_Notification)#394 (8) { ["id"]=> string(3) "297" ["item_id"]=> string(3) "242" ["secondary_item_id"]=> string(1) "1" ["user_id"]=> string(1) "1" ["component_name"]=> string(8) "messages" ["component_action"]=> string(11) "new_message" ["date_notified"]=> string(19) "2015-07-20 20:11:46" ["is_new"]=> string(1) "0" }
The actual Post ID in question is 756.
- The topic ‘Get Post ID from Notification’ is closed to new replies.