1

i have been trying to use draft_to_publish & new_to_publish
to create an update when ever a post of a specific type (cpt) is published...

it works but published 600+ times instead of one time!

So... my question is

  1. why does this happen & how to verify it only posts once
    (took me 10 minutes to delete)

here is code i currently have:

add_action('new_to_publish', 'new_webnews_published');
add_action('draft_to_publish', 'new_webnews_published');
function new_webnews_published($post_id){

    $postData       =   get_post($post_id);
    $post_id        =   $post_id;
    $postData       =   get_post($post_id);
    $postType       =   get_post_type($post_id);
    $postTitle      =   $postData->post_title;


    if($postType == 'webnews') {

        // LINK TO WEBNEWS PAGE
        $webnewsPage    =   of_get_option(THEME_NAME.'_webnews_select_page');   

        // PREPARE UPDATE DATA
        $update_title   =   __('News',THEME_NAME);
        $update_content =   $postTitle;
        $update_url     =   'index.php?p='.$webnewsPage;


        $post = array(
            'post_title'    => $update_title, 
            'post_status'   => 'publish',
            'post_type'     => 'miniupdate'
        );

        $updatedid  = wp_insert_post($post, 10, 1);

        // Do the wp_insert_post action to insert it
        do_action('wp_insert_post', 'wp_insert_post', 10, 1); 
        update_post_meta($updatedid, THEME_PREF.'miniupdates_content', $update_content);
        update_post_meta($updatedid, THEME_PREF.'miniupdates_link', $update_url);

    }
}

.
Last Thing: i would like to test variables
example: "echo 'something';

i guess i need to create a second function and attach to admin_init?
But what are the timing restriction here? should i add something to the
initial draft_to_publish hook ?

1

OK! found how to - there is 2 little information about
how to attach a function to a status change but this hook
does the job and does it well.

function miniupdate_webnews( $new_status, $old_status, $post ) {
    if ( $old_status == 'draft' && $new_status == 'publish' ) {

         // YOUR FUNCTION HERE...

    }
}
add_action( 'transition_post_status', 'miniupdate_webnews', 10, 3 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.