Support

Account

Home Forums General Issues Front end posting to multiple blog

Solved

Front end posting to multiple blog

  • Hi,
    I’m using wordpress multisite and acf latest version.
    I’m using this method for front end posting.

    <?php 
    
    function my_pre_save_post( $post_id )
    {
        // check if this is to be a new post
        if( $post_id != 'new' )
        {
            return $post_id;
        }
    
        // Create a new post
        $post = array(
            'post_status'  => 'draft' ,
            'post_title'  => 'A title, maybe a $_POST variable' ,
            'post_type'  => 'post' ,
        );  
    
        // insert the post
        $post_id = wp_insert_post( $post ); 
    
        // update $_POST['return']
        $_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );    
    
        // return the new ID
        return $post_id;
    }
    
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    
    ?>

    I want to post multiple blog at same time with this type of code.

    <?php
    $original_blog_id = get_current_blog_id(); // get current blog
    
    $bids = array(1,2); // all the blog_id's to loop through
    foreach($bids as $bid):
           switch_to_blog($bid); //switched to blog with blog_id $bid
           // ... your code for each blog ...
    endforeach ; 
    
    switch_to_blog( $original_blog_id ); //switched back to current blog
    ?>

    Please help me. I’m a newbie… Thanks in advance.

  • Hi @safeer N

    Interesting. Can you please take a look at the source code for the acf_form_head function located in /core/api.php

    You will see that to save the $_POST data, a simple action is called.

    You can use this code to run the save action within your loop.

    Thanks
    E

  • Hi ,
    Thank you for your reply. I saw that api.php file & save action. but I can’t understand what will I change. Is this way correct?

    $bids = array(1, 2);
    foreach ($bids as $bid) :
    	switch_to_blog($bid);
    	do_action('acf/save_post', $post_id);
    endforeach;
    switch_to_blog( $original_blog_id );

    Where I Put save function? Please help me. I can post to multiple blog at same time using wp_insert_post($post). but that time acf field not saving. Please…

  • Hi @safeer N

    The code: do_action('acf/save_post', $post_id); will save all $_POST field data to the post with ID $post_id.

    So within your loop, you will need to write some WP code to insert a new post, get the ID from that and then run the above code on each new post.

    Does that help?

    Thanks
    E

  • Hi @elliot

    Thanks a lot. That idea solve my problem.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Front end posting to multiple blog’ is closed to new replies.