4

Is there any hook which is fired only when the post is "published" for the first time.

I dont want to execute my code when the post is "updated" or when its status is set to "not-published" and then "published" again.

EDIT:

add_action('draft_to_published','func_tion');

function func_tion($post){
    $post_id = $post->ID;   

    if ( !get_post_meta( $post_id, 'mycoderan', $single = true ) )
    // run code to manipulate data
    wp_enqueue_script('plugin_name',plugin_dir_url(__FILE__ ).'script.js');
    $params = array() // set parameters
    wp_localize_script('plugin_name', 'params', $params );
    update_post_meta( $post_id, 'mycoderan', true );
}
3
  • You have to enclose everything in the if statement inside { ... } braces. w3schools.com/php/php_if_else.asp
    – soulseekah
    Apr 1, 2012 at 17:01
  • I need similar functionality, but can't figure out how to check if a post is being published for the first time or not. draft_to_published action seem to be triggered every time a post is published from draft multiple times. Are you relying on post meta to check if a post is published for the 1st time. Is there any native WP function that can be used instead?
    – dashaluna
    May 1, 2013 at 17:33
  • @dashaluna Please review the answer to my other question. wordpress.stackexchange.com/questions/46783/… With Wordpress 3.5, there have been a few changes so what you've written is also possible. The flow script should be tremendous help in this. Helped me alot
    – BAU
    May 3, 2013 at 17:50

1 Answer 1

5

The {$old_status}_to_{$new_status} and {$new_status}_{$post->post_type} hooks tend to generally solve the problem.

To avoid running the code in case post status is changed to draft then published again (after already being published), implement a simple flag using the post_meta functionality.

Note: the updated hook should be 'draft_to_publish' instead of 'draft_to_published' however, the code below is not modified and should be if you plan to use in WP 3.5

add_action( 'draft_to_published', 'wpse47566_do_once_on_publish' );
function wpse47566_do_once_on_publish( $post ) {
    // check post type if necessary
    if ( $post->post_type != 'a_specific_type' ) return;

    $post_id = $post->ID;

    if ( !get_post_meta( $post_id, 'mycoderan', $single = true ) ) {
        // ...run code once
        update_post_meta( $post_id, 'mycoderan', true );
    }
}
8
  • my code should run on any category type so i dont think i need this line // check post type if necessary if ( $post->post_type != 'a_specific_type' ) return; i have written my query within this function to get the post data and then i have to run a script by passing the post data as parameters... your function requires the post as an argument... so is the following ok??
    – BAU
    Apr 1, 2012 at 15:13
  • add_action( 'draft_to_published', 'func_tion' ); function func_tion() { /* query code */ if ( !get_post_meta( $post_id, 'mycoderan', $single = true ) ) // ...run code once update_post_meta( $post_id, 'mycoderan', true ); }
    – BAU
    Apr 1, 2012 at 15:16
  • the {$status}_to_{$status} action passes on the $post object, so $post_id = $post->ID, fixed my answer.
    – soulseekah
    Apr 1, 2012 at 15:17
  • i'm sorry i didnt know comments cannot process new-lines... i have only written the query to get post data inside the function... then i enqueue and localize the script inside the // run code once block... is that ok??
    – BAU
    Apr 1, 2012 at 15:18
  • i have edited the question... please let me know why my script is not executing.. where am i going wrong??
    – BAU
    Apr 1, 2012 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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