Skip to:
Content
Pages
Categories
Search
Top
Bottom

get_post_meta doesn’t work in activity stream


  • Hendrik Lersch
    Participant

    @riddler84

    Hello, i try to customize my activity stream items for a custom post type. I want to display a custom field as activity content, but the function get_post_meta() didn’t seem to work.

    Here’s my code so far. It’s basically the one from the documentation:

    function record_cpt_activity_content( $cpt ) {
        if ( 'new_guide' === $cpt['type'] ) {
        	$cpt['content'] = get_post_meta( $cpt['secondary_item_id'], 'guide_description', true );
        }
        return $cpt;
    }
    add_filter('bp_before_activity_add_parse_args', 'record_cpt_activity_content');

    I can use a simple string as content and it appears on new activitys. Also other functions like get_permalink( $cpt['secondary_item_id'] ); are no problem.

    But I can’t get get_post_meta() to work. Am i missing something?

Viewing 4 replies - 1 through 4 (of 4 total)
  • There is no reason why a call to get_post_meta should not work. Are you certain the post ID you are passing to it is valid, and that the record with the meta_key, exists?


    Hendrik Lersch
    Participant

    @riddler84

    I cannot actually test it, but i guess it’s eventually a timing problem. Maybe the post meta data isn’t already stored into the database, when the activity is added.

    Something like this:

    1. Post is stored into the database
    2. bp_activity_add() creates the activity
    3. post_meta is stored into the database

    The variable $cpt['secondary_item_id'] is holding the correct post_id. I have checked it. But maybe the post_meta isn’t there, when bp_activity_add() is called.

    I will test it later with an already existing post.

    I have also tried to set the filter priority to a higher value, so it’ll be called later, but that didn’t work too. :/


    Hendrik Lersch
    Participant

    @riddler84

    So.. i have test it with the ID of an already existing post and the meta_value show up in the newly created activity.

    $existing_post_id = 12345;
    
    $cpt['content'] = get_post_meta( $existing_post_id, 'guide_description', true );

    Then i have tried it again with the dynamically generated ID and the meta_value doesn’t show up. If i put this ID also right behind the get_post_meta function, only the ID will be visible, but not the meta_value.

    $cpt['content'] = get_post_meta( $cpt['secondary_item_id'], 'guide_description', true ) . $cpt['secondary_item_id'];

    Any Ideas? I don’t know how to fix this..


    Hendrik Lersch
    Participant

    @riddler84

    Sry, i would have edit my previous post, but there’s no option 🙂

    I have an addendum. If i save the post, before i publish it, the meta_value is stored in the database and is also present in the activity item.

    But if i publish it directly, the meta_value is obviously not saved, when the activity is created, so my theory from above seems right.

    Maybe it’s important to mention, that i save the posts for this CPT from the Frontend. First i call the function wp_insert_post and immediately afterwards i call update_post_meta to add or update the corresponding meta values.

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