Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] custom post type – activity stream thumbnail


  • shanebp
    Moderator

    @shanebp

    I’m trying to add the thumbnail for a custom post type into the activity entry when the post is created.

    
    add_filter( 'bp_blogs_activity_new_post_content', 'record_cpt_activity_content',  1, 3 );
    function record_cpt_activity_content($activity_content, $post, $post_permalink){
    if( $post->post_type == 'somecpt' ) {
    $postid=$post->ID;
    
    $activity_content = get_the_post_thumbnail( 1281, 'thumbnail' );
    //$activity_content = get_the_post_thumbnail( $postid, 'thumbnail' );
    
    $activity_content .= "test..." . bp_create_excerpt( $post->post_content, 25 );
    }
    return $activity_content;
    }
    

    If I hard code the post_id, it includes the thumbnail – but of course the wrong thumb.

    Why won’t get_the_post_thumbnail work in the filter function?

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

  • shanebp
    Moderator

    @shanebp

    If I include $postid in the $activity_content, it appears in the activity item and is correct.
    `
    $activity_content = ‘post_id: ‘ . $postid . ‘ + ‘ . bp_create_excerpt( $post->post_content, 25 );
    `

    So why isn’t get_the_post_thumbnail returning the image ?

    Maybe the thumb doesn’t exist at that point ?

    I’m trying to do this in a plugin file.

    ‘thumbnail’ is part of my supports array for register_post_type.
    And I use get_the_post_thumbnail in the edit screen (template file) for this cpt and it works fine.


    4ella
    Participant

    @4ella

    Hi , sorry I am not able to help you with your thumbnail problem but at least I will bump this thread for you and will ask you how did you get custom post type into activity page ? I have also created custom post type section (with the help of gravity forms form which generate a custom post type post which I see in standalone page) but I can’t get to see it in activity page too, can you post here the code to appear the content of custom post type in the activity page?


    shanebp
    Moderator

    @shanebp


    Roger Coathup
    Participant

    @rogercoathup

    @shanebp — from what you said in your OP, get_the_post_thumbnail() is working correctly (if you provide a hardcoded post_id) — so that function would appear not to be the problem.

    What output are you getting from get_the_post_thumbnail? I’d be var_dumping $post just before the call, and also $activity_content just after to narrow it down.

    @4ella — Shane has already given the code for doing that in his original post. If you don’t want any custom content, but simply want to add your custom post type to the standard types processed, you can simply add a filter to bp_blogs_record_post_post_types to include your cpt – something like:

    `

    add_filter( ‘bp_blogs_record_post_post_types’, ‘inspired_record_more_types’ );

    function inspired_record_more_types( $types ) {
    $types[] = ‘my-cpt-name’;

    return $types;
    }
    `


    shanebp
    Moderator

    @shanebp

    @rogercoathup

    var_dump on get_the_post_thumbnail is
    string(0) “”

    So I suspect a timing issue… ?


    Roger Coathup
    Participant

    @rogercoathup

    Can’t see why it would be a timing issue if it works fine with a hardcoded ID.

    What’s in your $post structure at that point? Have you definitely set a thumbnail for the post in question?


    4ella
    Participant

    @4ella

    Thank you both for your links-codes, I will study everything tonight , @rogercoathup the code you’ve provided where do I have to add this filter ? In which file ? in function.php ? I have also seen @shanebp code in another thread which seems to be different (I am not an PHP expert at all), the same question here where should I add it and which code is better , safer ?


    Roger Coathup
    Participant

    @rogercoathup

    Yes, functions.php (or bp-custom.php)

    Neither is safer / better – they do different things. One customises the content, the other just processes the cpt the same way a normal blog post would be processed. It depends what you want yours to do, as to which filter to use.


    shanebp
    Moderator

    @shanebp

    The hardcoded ID is from a prior post – I just wanted to make sure get_the_post_thumbnail was working in the function.

    Here is the post dump from the activity-content filter:
    `
    object(stdClass)#545 (25) { [“ID”]=> int(1298) [“post_author”]=> string(1) “1”
    [“post_date”]=> string(19) “2012-09-06 05:48:45”
    [“post_date_gmt”]=> string(19) “2012-09-06 12:48:45”
    [“post_content”]=> string(4) “test”
    [“post_title”]=> string(9) “fghfghfgh”
    [“post_excerpt”]=> string(0) “” [“post_status”]=> string(7) “publish”
    [“comment_status”]=> string(4) “open” [“ping_status”]=> string(4) “open”
    [“post_password”]=> string(0) “” [“post_name”]=> string(9) “fghfghfgh”
    [“to_ping”]=> string(0) “” [“pinged”]=> string(0) “”
    [“post_modified”]=> string(19) “2012-09-06 05:48:45”
    [“post_modified_gmt”]=> string(19) “2012-09-06 12:48:45” [“post_content_filtered”]=> string(0) “” [“post_parent”]=> int(0) [“guid”]=> string(0) “” [“menu_order”]=> int(0)
    [“post_type”]=> string(10) “memberpost” [“post_mime_type”]=> string(0) “”
    [“comment_count”]=> string(1) “0” [“ancestors”]=> array(0) { } [“filter”]=> string(3) “raw” }
    `

    I have definitely set a thumbnail for each post – they show up in the edit screen.
    So maybe the thumbnail hasn’t been set when I try to filter the activity content ?

    I use this in functions.php to handle the featured image upload
    `
    function insert_attachment($file_handler,$post_id,$setthumb=’false’) {

    if ($_FILES[$file_handler] !== UPLOAD_ERR_OK) __return_false();

    require_once(ABSPATH . “wp-admin” . ‘/includes/image.php’);
    require_once(ABSPATH . “wp-admin” . ‘/includes/file.php’);
    require_once(ABSPATH . “wp-admin” . ‘/includes/media.php’);

    $attach_id = media_handle_upload( $file_handler, $post_id );

    if ($setthumb) update_post_meta($post_id,’_thumbnail_id’,$attach_id);
    return $attach_id;
    }
    `

    And call it like so:
    `
    $pid = wp_insert_post($new_memberpost);

    if ($_FILES) {
    foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$pid,true);
    }
    }
    `


    Roger Coathup
    Participant

    @rogercoathup

    which is the standard loop / function for doing it — we all ‘cut & paste’ that one from the same place (although, in our case, we default set thumb to ‘false’, and explicitly call it with true where appropriate).

    Yes, you are doing the wp_insert_post(), which I guess fires off the new post hook, before you add the attachments, so they are not set yet.

    Not sure, how wp-admin gets around that.


    modemlooper
    Moderator

    @modemlooper

    I might have some code that might help, I’ll post later. I was messing with BP external blogs plugin to add the group avatar to activity. Haven’t used it in awhile but I remember it overrides an activity avi per type


    shanebp
    Moderator

    @shanebp


    shanebp
    Moderator

    @shanebp

    Got it ! Thanks to a hint from @rogercoathup

    Do this:

    wp_insert_post with status set to ‘draft’

    Then handle file uploads and set the thumb

    Then wp_update_post and set the status to ‘publish’


    modemlooper
    Moderator

    @modemlooper

    Cool!


    4ella
    Participant

    @4ella

    Hello, I am succesfuly using @rogercoathup ‘s snippet in bp_custom.php to display custom post type activity, but I would like to use more then ONE CPTs in my activity stream, how can I use it for 2 or more Custom Post Types? Do I have to replicate this code or does exist some hack how to do it for multiple Types?

    add_filter( ‘bp_blogs_record_post_post_types’, ‘inspired_record_more_types’ );
    
    function inspired_record_more_types( $types ) {
    $types[] = ‘my-cpt-name’;
    
    return $types;
    }
Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘[Resolved] custom post type – activity stream thumbnail’ is closed to new replies.
Skip to toolbar