WordPress.org

Ready to get started?Download WordPress

Forums

Meta Box
[resolved] Meta Box fields not up to date inside save_post action (2 posts)

  1. jonnyhocks
    Member
    Posted 4 months ago #

    Hi guys,

    Just looking to add a small checkbox meta box which allows pages to automatically be added to a menu when the post/page is saved if it's checked.

    The problem I'm running into is that when I get the meta inside the action, it's the value BEFORE the save/update button was hit in WordPress. I've tested this out with the title and content etc and these all provide me with the correct data which was saved on the hitting of the button.

    Any help would be great, here is the code I'm using just to test it works:

    <?php
    
    function auto_add_page_to_menu( $post_id ) {
    
    	// If this is just a revision, don't do anything
    	if ( wp_is_post_revision( $post_id ) )
    		return;
    
    	$post_title = get_the_title( $post_id );
    	$post_url = get_permalink( $post_id );
    	$subject = 'A post has been updated';
    	$menu = get_post_meta($post_id, 'cff_add-to-menu', true);
    	if ($menu == 1) {
    		$added_to_menu = 'This page was added to menu';
    	} else {
    		$added_to_menu = 'This page is not to be added to the menu';
    	}
    
    	$message = "A post has been updated on your website:\n\n";
    	$message .= $added_to_menu . ": " . $post_url;
    
    	// Send email to admin.
    	wp_mail( 'email@address.com', $subject, $message );
    }
    add_action( 'save_post', 'auto_add_page_to_menu' );

    NOTE: I am using the default WP method for getting the post meta here, but I get the same result if I use the rwmb_meta methods.

    Many thanks in advance!

    https://wordpress.org/plugins/meta-box/

  2. jonnyhocks
    Member
    Posted 4 months ago #

    Found a way around it by accessing the value via $_REQUEST as demonstrated in the save_post documentation in the Codex.

Reply

You must log in to post.

About this Plugin

About this Topic