Skip to:
Content
Pages
Categories
Search
Top
Bottom

Get Post ID from Notification

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

  • creativesinside
    Participant

    @creativesinside

    The goal being … I want to make a clickable link in the notification to go to the post corresponding to the notification.


    creativesinside
    Participant

    @creativesinside

    Or how can I add a column in the database table bp_notifications for post_id?

    Thanks!


    creativesinside
    Participant

    @creativesinside

    I have tried about 100 different things and I thought this would work:

    get_post_meta($post->ID,'thread_id');


    Henry Wright
    Moderator

    @henrywright

    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?


    creativesinside
    Participant

    @creativesinside

    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. 😀


    creativesinside
    Participant

    @creativesinside

    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();
    }

    creativesinside
    Participant

    @creativesinside

    Didn’t mean to post this.


    Henry Wright
    Moderator

    @henrywright

    The post ID should be passed to that function as either item_id or secondary_item_id. Then, if you have the notification ID, you can retrieve the data (post ID) at a later date with bp_notifications_get_notification()


    creativesinside
    Participant

    @creativesinside

    Wicked awesome! Going to check it out now.


    creativesinside
    Participant

    @creativesinside

    $notificationpostid = bp_notifications_get_notification(secondary_item_id);


    Henry Wright
    Moderator

    @henrywright

    Just to note, bp_notifications_get_notification() returns an BP_Notifications_Notification object so to get item_id you’d just access it like this:

    $obj = bp_notifications_get_notification( $id );
    echo $obj->item_id;

    creativesinside
    Participant

    @creativesinside

    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' => ,

    creativesinside
    Participant

    @creativesinside

    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?


    creativesinside
    Participant

    @creativesinside

    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>


    creativesinside
    Participant

    @creativesinside

    function allofdalights(){
    $obj = bp_notifications_get_notification( $id );
    echo $obj->item_id; }

    Uniprint QLD
    Participant

    @uniprint-qld

    Thanks from the team at Uniprint QLD. We managed to fix our code after reading this thread.

    Great job guys – all working again : )


    creativesinside
    Participant

    @creativesinside

    What’d you do to fix your code? I’m still stuck! 😀


    Henry Wright
    Moderator

    @henrywright

    You’ll need to replace $id in my snippet with the actual ID of the notification. You should be able to get that with bp_get_the_notification_id() if you’re using my snippet within the loop.


    creativesinside
    Participant

    @creativesinside

    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.


    Henry Wright
    Moderator

    @henrywright

    You shouldn’t need to do anything to bp-notifications-functions.php so revert those changes an your code should be good to go.


    creativesinside
    Participant

    @creativesinside

    After reverting the file … it’s still just displaying the “notification id.”


    Henry Wright
    Moderator

    @henrywright

    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 );

    creativesinside
    Participant

    @creativesinside

    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 }


    Henry Wright
    Moderator

    @henrywright

    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?


    creativesinside
    Participant

    @creativesinside

    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.

Viewing 25 replies - 1 through 25 (of 29 total)
  • The topic ‘Get Post ID from Notification’ is closed to new replies.
Skip to toolbar