Skip to:
Content
Pages
Categories
Search
Top
Bottom

Using get_post_meta() in activity stream


  • lonepalmus
    Participant

    @lonepalmus

    Hi guys,

    I am using buddypress 1.9.2 with wordpress 3.8.1 (no link atm, as it is localhost).

    I thought it would be kinda straight foreword, but for some reason this isn’t working for me. I am hooking into bp_blogs_activity_new_post_action and trying to display a custom field for a post.

    When I write get_post_meta($post->ID, 'key', true) it doesn’t output anything. But when echoing out the ID by itself, it displays fine, so it should be able to get the post meta, right?

    Does anyone know if this is possible?

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

  • shanebp
    Moderator

    @shanebp

    Are you sure the post meta exists?
    And exists at the moment you are calling it?

    If your code is only a few lines, paste it here.
    Otherwise make a gist.

    [ You have Site Tracking selected here: /wp-admin/options-general.php?page=bp-components ?]


    Henry Wright
    Moderator

    @henrywright

    get_post_meta($post->ID, 'key', true) won’t output, it just returns a string. You’ll need to echo it.


    lonepalmus
    Participant

    @lonepalmus

    Thanks for the replies!

    so in this case I want to use the metadata to change the content-part of the activity-stream:

    add_filter( 'bp_blogs_activity_new_post_content', 'record_post_activity_content', 1, 4 );
    
    function record_post_activity_content($activity_content, $post, $post_permalink, $post_title){
        if( $post->post_type == 'workout' ) {
         	$activity_content = get_post_meta( $post->ID, 'rute', TRUE );   
        }
            return $activity_content;
    }

    if I inside that function echo $post->ID; it shows the id fine – so it should be able to get the meta?


    Henry Wright
    Moderator

    @henrywright

    Try:

    add_filter( 'bp_blogs_activity_new_post_content', 'record_post_activity_content', 1, 3 );
    
    function record_post_activity_content( $activity_content, $post, $post_permalink ){
        if( $post->post_type == 'workout' ) {
         	$activity_content = get_post_meta( $post->ID, 'rute', TRUE );   
        }
            return $activity_content;
    }

    lonepalmus
    Participant

    @lonepalmus

    yeah, I know the $post_title isn’t really being used in this case. I just forgot to remove it. Unfortunately it doesn’t make a difference, even though i remove it.

    Could it be that the post-meta isn’t available at this point? Though i don’t see why it shouldn’t be, when the title and content are.


    Henry Wright
    Moderator

    @henrywright

    @lonepalmus post meta should be available!

    As @shanebp pointed out Site Tracking – are you sure you have it enabled? If it isn’t enabled bp_blogs_activity_new_post_content won’t fire.


    shanebp
    Moderator

    @shanebp

    @lonepalmus – let’s assume you’ve cleared all the am-I-a-dummy hints suggested so far.

    The timing can be tricky on these things.
    You might try changing the priority so that the function runs later.

    Once I had an issue including a thumbnail in an entry for the activity stream upon the creation of a cpt.
    The solution was to save the cpt as a draft, do the image handling, set the cpt to publish and then create the activity entry – at which point the image was available.
    Your task is bit different, but it may be a hint.


    lonepalmus
    Participant

    @lonepalmus

    @henrywright and @shanebp site tracking is enabled. And just to be sure I double-checked 🙂


    @shanebp

    I tried changing the priority, it didn’t change anything though. I recall having read something similar at a point about having to save as a draft – will look into and see if I can get anything out of it 🙂

    Thanks a mil both for your time!


    Henry Wright
    Moderator

    @henrywright

    @lonepalmus Ah hang on, I think I understand why this might not be working.

    Looking in bp_blogs_record_post I don’t think custom post types are recorded.

    // Don't record this if it's not a post
    if ( !in_array( $post->post_type, apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) ) ) )
        return false;

    I can see you’re working with a custom post type workout so this probably explains why bp_blogs_activity_new_post_content doesn’t fire.


    Henry Wright
    Moderator

    @henrywright

    @lonepalmus cc @shanebp

    There is actually a Trac ticket open for this. Just came across it:

    https://buddypress.trac.wordpress.org/ticket/3460


    lonepalmus
    Participant

    @lonepalmus

    Thanks! But I registered the custom post type and it goes to the activity stream fine. The problem is the custom fields. I hook into bp_blogs_activity_new_post_content and I’m able to output the title and content fine. I ended up just using the post title in the activity stream, which is fine for now, but it would be great to be able to output some custom fields in the stream in the future.

    Thanks for your help!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Using get_post_meta() in activity stream’ is closed to new replies.
Skip to toolbar