• Resolved luisleguisamo

    (@luisleguisamo)


    What I’m trying to accomplish:
    I’m trying to use one custom field to add multiple values to the same Meta Key.

    However, for each time that I submit a value, I get a duplicate, for example:

    Key1 = Sweet
    Key1 = Sweet
    Key1 = Yay
    Key1 = Yay

    And what I need help with is getting this result:
    Key1 = Sweet
    Key1 = Yay

    Here is my current code:
    I tried stripping the code down to the bare essentials, as I’m in the process of trying to understand what’s happening in each line of code. I removed the nonce code that’s found in the codex because, once again, it looked like trivial info for the crash course I intended to put upon myself. Be nice ??

    add_action( 'add_meta_boxes', 'ez_portfolio_gallery' );
    
    function ez_portfolio_gallery() {
    	add_meta_box( 'ez_portfolio_gallery', 'Portfolio Gallery', 'myplugin_inner_custom_box', 'post', 'normal', 'high'  );
    }
    
    function myplugin_inner_custom_box( $post ) {
    
    //i'm using this to output my current values in addition to what I can see in the custom fields section. For debugging purposes.
    	$ez_portfolio_image_name = get_post_meta($post->ID, 'ez_portfolio_image', true);
    
    	$custom_fields = get_post_custom(1);
      	$my_custom_field = $custom_fields['ez_portfolio_image'];
     	if($my_custom_field != ""){
     		foreach ( $my_custom_field as $key => $value ){
        		echo $key . " => " . $value . "<br />";
      		}
    	}
    
    	?>
    
    	<label>Image URL:</label>
    	<input type="text" id="ez_portfolio_image" name="ez_portfolio_image" value="" size="25" />
    
    	<?php
    }
    add_action( 'save_post', 'myplugin_save_postdata' );
    function myplugin_save_postdata() {
    
    	// Check permissions
    	if ( 'post' == $_POST['post_type'] ){
    
    			$ez_portfolio_image = $_POST['ez_portfolio_image'];
    
    			if($ez_portfolio_image != ""){
    				add_post_meta(1, 'ez_portfolio_image', $ez_portfolio_image, false);
    
    			}
    
    	}
    
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter luisleguisamo

    (@luisleguisamo)

    Resolved!

    I’ve pasted my solution here.

    What the problem was
    add_action('save_post','ez_portfolio_gallery_save');
    The hook save_post submits twice.

    While searching online I found out that save_post submits a regular post and a revision.

    Therefore, when I retrieved the post_type(i forgot what I used to do this) it was outputting “post, post”(i added the comma and space for clarity). This made things confusing because I was hoping to see something like “post, revision”(i added the comma and space for clarity).

    Therefore if I wanted to ignore the second post_type “revision” with some conditional statement, it would fail.

    The online forums mentioned wp_is_post_revision. However, for some reason a lot, I mean A LOT of people have developed a habit of not informing you how to use it with a simple example.

    How to use wp_is_post_revision within a conditional statement

    function ez_portfolio_gallery_save($post_ID){
    
            $ez_portfolio_gallery = $_POST['ez_portfolio_gallery'];
    
            //verify post is not a revision
            if ( !wp_is_post_revision( $post_ID ) ) {
    
                    add_post_meta($post_ID, 'ez_portfolio_gallery', $ez_portfolio_gallery);
    
            }
    }

    NOTE:
    You don’t have to use the global $post; code (found in the pastebin). I was simply using that for debugging purposes.

    I hope this helps others!

    Thankyou luisleguisamo. This was just what I wanted to know. And the background too. Very clear and helpful. Many thanks.

    Thread Starter luisleguisamo

    (@luisleguisamo)

    You’re welcome, conormaxwell! I’m glad it was helpful.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘add_post_meta One Key with Multiple Values’ is closed to new replies.