0

here is the scenario:

  • Custom Post Type "shows"
  • Custom Post Type "reviews"

Reviews are "child" of shows so that every review has a custom field with the ID of the parent "show".

Since a review has also a custom field with a rating value I'd like upon save to create/update a custom field for the parent post (show) with the average rating.

I create a function which returns a numeric value for the average rating.

I'm trying to use save_post action hook without success. Here is my code:

function save_show_rating( $post_id ) {

    /*
     * In production code, $slug should be set only once in the plugin,
     * preferably as a class property, rather than in each function that needs it.
     */
    $slug = 'reviews';

    // If this isn't a 'book' post, don't update it.
    if ( $slug != $_POST['post_type'] ) {
        return;
    }

    // Retrieving parent ID 
    $parent_id = get_post_meta( $post_id, '_wpcf_belongs_fulldome-shows_id', true );

    // Saving average rating
    $average = average_rating_num_func();

    // Check if the field already exists
    $custom_field = get_post_meta( $parent_id, 'average_rating', TRUE );

    // Update parent's metadata.
    if ( $custom_field != '') {
        update_post_meta( $parent_id, 'average_rating', $average );
    } else {
        add_post_meta($parent_id, 'average_rating', $average, true);
    }
}
add_action( 'save_post', 'save_show_rating' );

Hope you can help. Thank you guys

Best regards Carlo

  • 1
    Hello, Caletto. What have you done to troubleshoot this code; how do you know that it's not working? Can you confirm that the save_show_rating() function is firing when you save a review? Is average_rating_num_func() returning what you expect it to? Are you positive you registered your "reviews" post type using 'reviews' as the post-type key? – bosco May 30 '14 at 17:50
  • Hi @boscho . Thanks for your comment. Well actually I don't know if save_show_rating() is firing at the right time... for sure I check the database "wp_postmeta" table and not find any record for "average_rating" saved. The "Reviews" post type, as the "Shows", are created with WP-Types plugin and the slug I set for them has been checked several times to avoid ordinary mistakes. The average_rating_num_func() is returning a numeric value with 2 decimals and I already tried it inside a template file. What I don't say is the code I'm using is a sort of mod of the one you can find in the WP codex – carletto0282 May 31 '14 at 6:18
  • Alright, cool - now we have a starting point to work from. Another question: in what file is this code you've posted? The active theme's functions.php or a plugin perhaps? If you haven't done so already (and you're not in a production environment), enable WP's debug mode by adding this line to your wp-config.php before the "stop editing" comment: define( 'WP_DEBUG', true );. This will enable the output of WP debugging info & errors. – bosco Jun 2 '14 at 16:02
  • You can use PHP's die()/exit() function to kill script execution with a message. You should avoid using it in production code as it's messy and blunt, but it can be useful for debugging purposes. First let's check if the function is getting called when a post (of any type) is saved. At the top of your function before the if statement, add this line: die( 'Death at save_show_rating( ' . $post_id . ' )' );. – bosco Jun 2 '14 at 16:41
  • Now whenever save_show_rating() is called (which should be when the save_post WP action is fired), PHP should kill the script and display the message Death at save_show_rating( {value of $post_id} ). Now go into your dashboard and save a review - did PHP die() and display the message? If so, that implies that save_show_rating() is indeed getting called when it should be. – bosco Jun 2 '14 at 16:51

Your Answer

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

Browse other questions tagged or ask your own question.