0

I try to use the hook save_post in my wp but I have a problem. I do this :

function when_save_post_match($post_id, $post){

if (isset($post->post_status) && 'auto-draft' == $post->post_status) {
    return;
}

if( 'match' == $post->post_type ){

    $id_match = get_post_meta( $post_id, '_select_match_from_db', true );

    $req = $bdd->prepare('INSERT INTO posts_matchs (id_match,id_post) VALUES
        (
            :id_match,
            :id_post
        )
    ON DUPLICATE KEY UPDATE id_match = :id_match, id_post = :id_post');
    $req->bindParam(':id_match', $id_match);
    $req->bindParam(':id_post', $post_id);
    $req->execute();
    $req->closeCursor();
}
}
add_action('save_post', 'when_save_post_match', 10, 2);

When I create a custom post "match", the action of inserting data into the db is NOT DONE. When I update a custom post "match", the action IS DONE. When I create and then delete a post match, the action IS DONE.

I would like the action to be taken when I create a custom post match (and update) and just for the published post.

I don't understand how to do this.

Any idea ?

Thanks

EDIT

With transition post status :

function when_save_post_match( $new_status, $old_status, $post ) {
if ( ( 'publish' === $new_status && 'publish' !== $old_status ) && 'match' === $post->post_type) {

        $id_match = get_post_meta( $post->ID, '_select_match_from_db', true );

        $req = $bdd->prepare('INSERT INTO posts_matchs (id_match,id_post) VALUES
        (
            :id_match,
            :id_post
        )
    ON DUPLICATE KEY UPDATE id_match = :id_match, id_post = :id_post');
        $req->bindParam(':id_match', 32);
        $req->bindParam(':id_post', 20);
        $req->execute();
        $req->closeCursor();

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

This code does not work :-( and when I create a new match the mesage "publication failed" appears the first time I click on "publish" and desappear after clicking a second time on the "publish" button... Don't understand what Is going on

3
  • save_post is a misleading name - it fires a lot more than you would expect. Look into the post status transitions hooks instead. Either transition_post_status where you can specify what old and new status you want to trigger things on - i.e. if new_status is publish and old_status is not publish, you know it's a brand-new published post - or else something like publish_post which will fire for both brand-new published posts and anytime a published post is updated, only for the post post_type. codex.wordpress.org/Post_Status_Transitions
    – WebElaine
    Jun 30, 2022 at 17:02
  • Thanks I try with status but it does not work... I don't understand. Nothing in my table of my database when I publish or update a match cutom post. I edit my first message to display the code I used. Thanks
    – Gpi
    Jun 30, 2022 at 18:03
  • After more tests, I think the problem is not this : I want to intercept the data in a custom fields and insert it in a table in my database. When I publish it can't be possible because this field is not already in the post_meta table in WP. So it works when I update the post but not when I publish the post....
    – Gpi
    Jul 1, 2022 at 6:17

1 Answer 1

0

I think one key question here is how _select_match_from_id meta value is saved? Is it some metafield on the post edit screen or from some other logic?

If I may assume that it is from a metafield, then you could just read its value from the $_POST array.


You could also improve your code by splitting the callback function into two, one for new posts and the other for updates, and moving the database action into a separate function.

Using the save_post_{$post->post_type} helps you eliminate the post type check from the callback. The save_post callback recieves also a third parameter bool $update which you can use to determine, if it is a new post that is saved or an post update.

So you could for example do the following,

// Use post type specific actions to eliminate post type checking in callback
add_action('save_post_match', 'when_save_post_match', 10, 3);
add_action('save_post_match', 'when_update_post_match', 10, 3);

function when_save_post_match(int $post_id, WP_Post $post, bool $update) {
  // only new posts
  if ( $update ) {
    return;
  }
  // get data for new post
  $id_match = new_posts_match_from_db($post_id);
  // pass id and data to action
  execute_database_action($post_id, $id_match);
}

function new_posts_match_from_db(int $post_id) {
  // this could be from the posted values as below or some other logic
  // TODO: update to match your setup
  return $_POST['_select_match_from_db'] ?? null;
}

function when_update_post_match(int $post_id, WP_Post $post, bool $update) {
  // only updates
  if ( ! $update ) {
    return;
  }
  // get data from post meta
  $id_match = existing_posts_match_from_db($post_id);
  // pass id and data to action
  execute_database_action( $post_id, $id_match );
}

function existing_posts_match_from_db(int $post_id) {
  return get_post_meta( $post_id, '_select_match_from_db', true );
}

function execute_database_action(int $post_id, $id_match) {
  // your db action...
}

Modify as needed.

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.