• Hey guys!

    I ran into a problem. I created a custom post type, called background. A background post has a featured image, title, tags & a designer.
    Each background has the possibility to be set as current background. That means that the wp site reads out an option, called ‘my_background’, reads its value and calls a wp query to get the featured image.

    The solution I made to make it work is to set a new metabox with a checkbox in it. I managed to get the communication work between the option and the metabox, BUT!

    What I want to do is, as soon as it is posted, set the meta tag equal to 0, that means the checkbox is unchecked again. So there is no confusion about that many posts has the value checked. It’s like a one-time-function-only.

    Here is my code:

    function set_background( $post_ID ) {
    		global $posts, $post;
    
    		$option_name = 'my_backround' ;
    		$newvalue = $post_ID ;
    
    		//$postMeta = get_post_meta( $post_ID, '_cf_background_set', true );
    		//$newvalue = $postMeta;
    		update_post_meta( $post_ID, '_cf_background_set', 0, 1 );
    
    		if (isset($_REQUEST['_cf_background_set'])) {
    
    			if ( get_option( $option_name ) != $newvalue ) {
    			    update_option( $option_name, $newvalue );
    			} else {
    			    $deprecated = ' ';
    			    $autoload = 'no';
    			    add_option( $option_name, $newvalue, $deprecated, $autoload );
    			}
    		}
    	}
    	add_action('publish_background', 'set_background');
    	add_action('save_background', 'set_background');

  • The topic ‘update_post_meta not working in save_post’ is closed to new replies.