0

I'm trying to populate the postmeta table with a custom value when a custom type ('my-solutions') is published. I've read a few threads, the Codex and Pippin's blog on this matter and tried many variations, but can't get this to work. If anyone can add or correct anything that would be great.

function run_when_my_solution_published( $post ){
    global $wpdb;
    $id = $post->ID;
    if($post->post_type == 'my-solutions'){
        $wpdb->insert('wp_postmeta', array( 'meta_id'     => NULL       ,
                                            'post_id'     => $id        ,
                                            'meta_key'    => 'my_json'  ,
                                            'meta_value'  => 'json'     ),
                                     array( '%d' , '%d' , '%s' , '%s' )
        );


    }
}
add_action('new_to_publish', 'run_when_my_solution_published');     
2
  • Could you check the $post->post_status whenever a post is saved to see if it's published? There also seems to be a Publish Post hook "That's not deprecated!". – Howdy_McGee Jul 30 '14 at 16:19
  • Thanks for the bold note, I don't see a deprecated notice at codex.wordpress.org/%20Post_Status_Transitions . . . nevertheless, I've tried that 'publish_post' action and it doesn't seem to work either. – jimlongo Jul 30 '14 at 16:30
2

Newer New solution:

function wpse153622_transition_solution( $new_status, $old_status, $post ) {
    if ( $new_status != $old_status && 'publish' === $new_status && 'my-solution' === $post->post_type ) {
        update_post_meta( $post_id, 'my_json', 'json' );
    }
}
add_action(  'transition_post_status',  'wpse153622_transition_solution', 10, 3 );

This should trigger only when 1. the new post status isn't the same as the old one 2. new post status is equal to publish and 3. post type is equal to your post type.

New Solution

Edit: After reading up on post status transitions, I think this should do the trick:

function wpse153622_save_solution( $post_id, $post ) {
    update_post_meta( $post_id, 'my_json', 'json' );
}
add_action(  'publish_my-solution',  'wpse153622_save_solution', 10, 2 );

This uses publish_my-solution, which should only trigger whenever a post of type my-solution is published. See {status}_{post_type} here.

Old solution

Using the save_post_{post_type} action will the job for you, though it'll be called whenever you update the post as well as publish:

function wpse153622_save_solution( $post_id, $post, $update ) {
    update_post_meta( $post_id, 'my_json', 'json' );
}
add_action( 'save_post_my-solution', 'wpse153622_save_solution', 10, 3 );

I've used this before and it works, but like I said, it'll be called whenever you make changes to the post. I don't think that'll be much of an issue, though.

8
  • Thanks for your comment. I only want to add the data on first publication. I've tried new_to_publish_my-solution, publish_post_my-solution, publish_post, and others, but the only action I can get to fire so far is transition_post_status (I'm checking by sending a die() command). However I still can't get it to add_post_meta to the $post->ID – jimlongo Jul 30 '14 at 18:45
  • Added a new solution above the old one. This one does work. Just tested – Manny Fleurmond Jul 30 '14 at 18:56
  • Well the new solution will at least fire on Publish (with a die() command). Now if I could just get it to update the postmeta. – jimlongo Jul 30 '14 at 19:01
  • I've tested it with post as well as with other post types. It adds the post meta, as you wanted – Manny Fleurmond Jul 30 '14 at 19:02
  • Thanks again, still all these actions tend to fire on any save of the post. Even though the documentation clearly says it shouldn't, it is firing all the time. publish_my-solution – jimlongo Jul 30 '14 at 19:32
1

I was able to add / remove terms from a post type if it was published by using the transition_post_status hook. I've adapted your question to my solution below:

function run_when_my_solution_published( $new_status, $old_status, $post ) {
    global $wpdb;

    if($post->post_type == 'my-solutions' && $new_status == 'publish'){
        $wpdb->insert('wp_postmeta', array( 'meta_id'     => NULL       ,
                                            'post_id'     => $id        ,
                                            'meta_key'    => 'my_json'  ,
                                            'meta_value'  => 'json'     ),
                                     array( '%d' , '%d' , '%s' , '%s' )
        );
    }
}
add_action ('transition_post_status', 'run_when_my_solution_published', 999, 3);

Though The Codex says that it's not acceptable to use this hook, it was the only one I could get acutally working.

1
  • Actually the codex.wordpress.org/Post_Status_Transitions page says this is acceptable (not to be confused with the private function _transition_post_status. And the action fires, only it fires on every save of the post, not just intial publication. – jimlongo Jul 30 '14 at 19:34
0

As @Howdy_McGee has said, I'd actually use the publish_post action instead.

Sample code block to send an email on publish:

function post_published_notification( $ID, $post ) {
    $author = $post->post_author; /* Post author ID. */
    $name = get_the_author_meta( 'display_name', $author );
    $email = get_the_author_meta( 'user_email', $author );
    $title = $post->post_title;
    $permalink = get_permalink( $ID );
    $edit = get_edit_post_link( $author, '' );
    $to[] = sprintf( '%s <%s>', $name, $email );
    $subject = sprintf( 'Published: %s', $title );
    $message = sprintf ('Congratulations, %s! Your article “%s” has been published.' . "\n\n", $name, $title );
    $message .= sprintf( 'View: %s', $permalink );
    $headers[] = '';
    wp_mail( $to, $subject, $message, $headers );
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );

You can use more or less what you've already used here for filtering by post_type. I would also definitely recommend using the add_post_meta function instead of a custom $wpdb query.

2
  • I'm not against publish_action or add_post_meta if it would work. I've changed both of those and it still doesn't add the meta value to the database – jimlongo Jul 30 '14 at 16:47
  • Why not use save_post_my-solutions? – Manny Fleurmond Jul 30 '14 at 17:51

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.