BuddyDev

Search

BuddyBlog – How Post to different Site ID from BP

  • Participant
    Level: Initiated
    Posts: 5
    Jared Hungerford on #23834

    Hi,

    I currently have a multi-site environment and I am trying to figure out how to have buddyblog posts ONLY post to a separate Site ID other than the one that BuddyPress is installed on. How can this be achieved?

    For example, even though all BP info is private in Site ID 2, I would like to have member blog posts to post only to Site ID 4 but also be available publicly.

    Main Site – ID 1
    BuddyPress – ID 2
    Marketplace – ID 3

    Member Blogs – ID 4

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #23873

    Hi Jared,

    Thank you for the topic. It seems, we may achieve it using switch_to_blog and restore restore_current_blog.

    Please allow us to test it as a proof of concept next week and get back to you.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    Jared Hungerford on #23877

    Awesome! I am hoping for good results.

    *fingers crossed*

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #24047

    Hi Jared,
    Thank you for your patience.

    Here is the code that you can put in your bp-custom.php

    
    
    /**
     * When a post is published, create a new entry on the secondary blog.
     *
     * @param string  $new_status nes status.
     * @param string  $old_status old status.
     * @param WP_Post $post Post object.
     */
    function buddydev_propagate_post_to_other_blog( $new_status, $old_status, $post ) {
    
    	$primary_id          = 1; // change with yours main site where BuddyPress is active.
    	$publish_blog_id     = 2; // Change with the blog id where to publish.
    	$buddyblog_post_type = 'post'; // change with the post type.
    
    	$post_id = $post->ID;
    	if ( get_current_blog_id() != $primary_id ) {
    		return;
    	}
    
    	if ( $new_status == $old_status || $new_status !== 'publish' || $post->post_type != $buddyblog_post_type ) {
    		return;
    	}
    
    	// if we are here, switch blog and insert post.
    	switch_to_blog( $publish_blog_id );
    	$post = clone $post;
    	unset( $post->ID );
    	$id = wp_insert_post( get_object_vars( $post ) );
    	restore_current_blog();
    	// Keep a reference for future usage.
    	if ( $id && ! is_wp_error( $id ) ) {
    		update_post_meta( $post_id, '_mapped_post_id', $id );
    	}
    }
    
    add_action( 'transition_post_status', 'buddydev_propagate_post_to_other_blog', 10, 3 );
    
    

    Please change the primary id, publish blog id and post type as you need.

    When a post is published, It creates an entry on the secondary blog.

    Please do note that if a post is published then unpublished and then published again, It will create two entries.
    You can improve it to handle that(I have stored the mapped post id in meta). It is a basic example to show how it works.

    PS:- If you are using custom post type with BuddyBlog, That must be available on your secondary blog too.

    Hope that helps.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    Jared Hungerford on #24048

    Oooohhh. I’m excited to test this out!!!

    Will keep you updated on progress. Greatly appreciate the time spent to look into this.

    🙂

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #24049

    Sure, Please give it a shot and let me know how it goes 🙂

  • Participant
    Level: Initiated
    Posts: 6
    Ritz on #32651

    i tried this. it does fine
    Thanks for the code.
    but i dont want the post to be cloned. i just need it to be posted to other site.
    is it possible to actually load buddyblog on publish_blog_id ; that way even the post that are shown comes from publish_blog and not current_blog

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #32663

    Hi Ritz,
    Please open a different topic and provide more clear details. I am not sure if I understand your use case, If I do, I will have better feedback.

    Regards
    Brajesh

The topic ‘BuddyBlog – How Post to different Site ID from BP’ is closed to new replies.

This topic is: not resolved