Support » Plugin: WPSiteSync for Content » Execute a function in target site after sync is done

  • Resolved Kim Soler


    I want to execute a function after a post is inserted/updated in the target site to assign new categories to all posts that have a category at source site.

    I have tried with this hooks but don’t seem to work:

    add_action('wp_insert_post', 'update_post_cats');
    add_action('the_post', 'update_post_cats');
    add_action('draft_to_publish', 'update_post_cats');
    add_action('new_to_publish', 'update_post_cats');
    add_action('pending_to_publish', 'update_post_cats');
    add_action('future_to_publish', 'update_post_cats');
    add_action('publish_post', 'update_post_cats');
    add_action('post_updated', 'update_post_cats');

    If I update an imported post in the target site it assigns ok the new category, but the main idea is to make this once the post is synchronized.

    There’s some hook to make this or you know some way to achieve this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author ServerPress


    Hello, Kim Soler.

    Thank you for your interest in WPSiteSync.

    Most of those action hooks are triggered when editing a Post in the admin, so I’m not surprised you’re not seeing those work.

    After processing, WPSiteSync does trigger its’ own hook and you can use that. It’s called ‘spectrom_sync_push_content’ and has three parameters:
    $target_post_id – the post ID that was updated
    $post_data – an array of data for the post. This does include all of the taxonomies for the post on the Source site. Or, you can get the terms using the $target_post_id
    $response – a SyncApiResponse object

    So you can use something like this:
    add_action( ‘spectrom_sync_push_content’, ‘my_callback’, 10, 3 );
    function my_callback( $post_id, $post_data, $response )
    // do your work here

    Hope that helps.

    All the best,
    The ServerPress Team

    Hi @serverpress,

    This code must go in source or target site?

    Plugin Author ServerPress


    Hello Kim Soler,

    The code can go on both Source and Target. The action hook is only ever triggered on a Target site, after the Push operation is complete. So it only needs to be on the Target. It won’t do anything on the Source site.

    All the best,
    The ServerPress Team

    I have tried this but it doesn’t seem to work:

    add_action( 'spectrom_sync_push_content', 'update_post_cats', 10, 3 );
    function update_post_cats( $post_id, $post_data, $response ) {
    	$categories = wp_get_post_categories( $post_id );
    	if(has_category(array('source-site-category-1', 'source-site-category-2', 'source-site-category-3'))) {
    		$newcat = get_term_by( 'slug', 'target-site-category-1', 'category' );
    		array_push( $categories, $newcat->term_id );
    Plugin Author ServerPress


    Hi Kim Soler,

    I see a problem in your code. There is no “current post” context since the WPSiteSync’s Push operation happens essentially “outside the loop”. This means you need to provide a second parameter to the has_category() function. Pass it the $post_id as the second parameter so it knows which post you’re getting the categories from.

    You’re also not setting the new category for the post. You probably want to use something like wp_set_post_categories($post_id, $categories) for that.

    I hope that helps.

    All the best,
    The ServerPress Team

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.