Skip to:
Content
Pages
Categories
Search
Top
Bottom

Recent Blog Posts – duplicate posts


  • yu
    Participant

    @gerbilo

    Hey guys! I’ve got strange bug in Recent Blog Posts widget. After editing post by anyone (including author) this widget gets copy of this post in output. Please see it on http://tf2.lv

    And also this widget gets post with NON PUBLISH status!

    +++

    I hope somebody could edit this fuction to help me with that problem ->

    function get_latest_posts( $blog_id = null, $limit = 5 ) {

    global $wpdb, $bp;

    if ( $blog_id )

    $blog_sql = $wpdb->prepare( ” AND p.blog_id = %d”, $blog_id );

    $post_ids = $wpdb->get_results( $wpdb->prepare( “SELECT p.post_id, p.blog_id FROM ” . $bp . ” p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.public = 1 AND b.deleted = 0 AND b.archived = ‘0’ AND b.spam = 0 AND b.mature = 0 $blog_sql ORDER BY p.date_created DESC LIMIT $limit” ) );

    for ( $i = 0; $i < count($post_ids); $i++ ) {

    $posts[$i] = BP_Blogs_Post::fetch_post_content($post_ids[$i]);

    }

    return $posts;

    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • The duplicate posts show up because a different user edited the post, I’m not sure what this is used for, maybe to show posts you contributed to or something..

    Anyway, to remove the duplicate posts all you need to do is add one word to the SELECT statement. Just add DISTINCT between “SELECT” and “p.post.id” so it looks like this: SELECT DISTINCT p.post_id, p.blog_id FROM…

    That will have the query return only one result for the post even though there are two rows in the table.

    The problem with the post showing up even if it is NOT published is because the post was previously save as “published” and has been changed to “unpublished”. The code currently returns if the status is unpublished so the existing entry never gets removed. To fix this you’ll need to edit a few lines in bp-blogs.php.

    Around line 268

    Change this:

    if ( !$post || $post->post_type != 'post' || $post->post_status != 'publish' || $post->post_password != ''")
    return false;

    To this:

    if ( !$post || $post->post_type != 'post')
    return false;

    This change is need to keep us in the process so we can update an existing entry if it’s status has changed.

    Around line 384

    Change this:

    /* Don't record this if it's not a post, not published, or password protected */
    if ( $post->post_type != 'post' || $post->post_status != 'publish' || $post->post_password != '' )
    return false;

    /**
    * Check how many recorded posts there are for the user. If we are
    * at the max, then delete the oldest recorded post first.
    */
    if ( BP_Blogs_Post::get_total_recorded_for_user() >= TOTAL_RECORDED_POSTS )
    BP_Blogs_Post::delete_oldest();

    if ( !BP_Blogs_Post::is_recorded( $post_id, $blog_id ) ) {
    if ( $post->post_status == 'publish' ) {
    $recorded_post = new BP_Blogs_Post;

    To this:

    /* Don't record this if it's not a post */

    if ( $post->post_type != 'post' )
    return false;

    if ( !BP_Blogs_Post::is_recorded( $post_id, $blog_id ) ) {
    if ( $post->post_status == 'publish' ) {
    /* gwrey
    Moved the test for max total records inside this
    block so we will only delete a record IF we are going to add a new one
    */
    /**
    * Check how many recorded posts there are for the user. If we are
    * at the max, then delete the oldest recorded post first.
    */
    if ( BP_Blogs_Post::get_total_recorded_for_user() >= TOTAL_RECORDED_POSTS )
    BP_Blogs_Post::delete_oldest();

    $recorded_post = new BP_Blogs_Post;
    $recorded_post->user_id = $user_id;

    Also needed so we keep going and update any existing entries

    Around line 419

    Change this:

    if ( $post->post_status != 'publish' )
    BP_Blogs_Post::delete( $post_id, $blog_id );

    To this:

    if ( $post->post_status != 'publish' || $post->post_password != '')
    BP_Blogs_Post::delete( $post_id, $blog_id );

    This will delete a post from the BP posts log if it is no longer published or if a password has been added.

    Around line 423

    Change this:

    add_action( 'publish_post', 'bp_blogs_record_post' );

    To this:

    add_action( 'save_post', 'bp_blogs_record_post' );

    This will update the post data any time it is saved, not just when published.


    yu
    Participant

    @gerbilo

    about DISTINCT select i got it on my own, but thnx anyway ) but for publish->unpublish fix HUGE respect )) maybe you know how to fix ‘duplicated members’ in groups? )


    altesc
    Participant

    @altesc

    Hello, I have the same problem but I don’t understand where I have to change the code. Which PHP file has to be modified? Thanks.


    Anonymous User 303747
    Inactive

    @anonymized-303747

    I created a ticket for the duplicate post in widget issue – seems like it hadn’t been reported previously.

    https://trac.buddypress.org/ticket/356

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Recent Blog Posts – duplicate posts’ is closed to new replies.
Skip to toolbar