get_post_meta not working in bp_before_activity_add_parse_args
-
I have several custom post types registered that I am adding into the activity stream. I am trying to use bp_before_activity_add_parse_args() to customise the content displayed in the activity stream.
When trying to capture custom fields from the post to add alongside the post title, it doesn’t work.
However if I save a draft of the post first then it does work.
My code is as below:
/* function to pull content into activity feed from post */ function record_cpt_link_activity_content( $cpt ) { $meta = get_post_meta($cpt['secondary_item_id']); /*print '<pre>'; print_r($meta); print '</pre>'; exit;*/ /* // the above array $meta is empty (UNLESS I SAVE A DRAFT FIRST!) and displays the below: Array ( [_edit_lock] => Array ( [0] => 1491208194:1 ) [_edit_last] => Array ( [0] => 1 ) ) */ $cpt['content'] = '<br /><a href="'.get_post_meta($cpt['secondary_item_id'], 'url', true).'">'.get_the_title($cpt['secondary_item_id']).'</a>'; return $cpt; } add_filter('bp_before_activity_add_parse_args', 'record_cpt_link_activity_content');
It seems like a timing issue.
Not that this topic documents the exact same problem however I couldn’t reply there as it’s closed to new replies…
-
I’ve managed to work around this by using the bp_get_activity_content_body filter.
However I’m sure I should be able to use bp_before_activity_add_parse_args, as the newly created custom post id exists at that point in the secondary_item_id array key…
/* functions to pull content into activity feed from post */ function record_cpt_activity_content( $cpt ) { // get_post_title($cpt['secondary_item_id']) works. // get_post_meta($cpt['secondary_item_id'], 'YOUR_META_KEY_HERE', true) doesn't... if ('YOUR_CUSTOM_POST_TYPE' == $cpt['type']) { // need to add at least a space here to the content otherwise the filter bp_get_activity_content_body doesn't seem to work... $cpt['content'] = ' '; } return $cpt; } add_filter('bp_before_activity_add_parse_args', 'record_cpt_activity_content'); function filter_cpt_activity_content( $content, $activity ) { if ( isset( $activity->secondary_item_id ) && 'YOUR_CUSTOM_POST_TYPE' == $activity->type) { $current_post = $activity->secondary_item_id; // trim any white space added by record_cpt_activity_content() $content = trim($content); // add in our custom content $content .= get_post_meta($current_post, 'YOUR_META_KEY_HERE', true).'">'; } return $content; } add_filter( 'bp_get_activity_content_body', 'filter_cpt_activity_content', 1, 2 );
Obviously replace YOUR_CUSTOM_POST_TYPE and YOUR_META_KEY_HERE.
- You must be logged in to reply to this topic.