Video Tutorial Metadata


In this chapter we will see how to manage the metadata associated with an article and how to create an interface to administer it. Metadata is used to save additional information.

// To add a meta
update_post_meta ($ postId, 'my_meta', 'my value');

// To update or add a meta
update_post_meta ($ postId, 'my_meta', 'my value');

// To delete a meta
delete_post_meta ($ postId, 'ma_meta');

// Get metadata
get_post_meta ($ postId, 'ma_meta');

To be able to update the information we will add a "metabox" which will be displayed in the administration interface. We will use 2 actions:

  • add_meta_boxes, allows to save our box and will allow a function to declare the boxes to add via the function add_meta_box ()
  • save_post, will add to the backup of the article and set up the persistence of information.

Here is a sample code, as a class, to get an idea of ​​how the process works in general.

ID, self :: META_KEY, true);
        wp_nonce_field (self :: NONCE, self :: NONCE);
        ?>
        <input type = "hidden" value = "0" name = "">
        <input type = "checkbox" value = "1" name = "" >
        
        <? php
    }

    public static function save ($ post) {
        if (
            array_key_exists (self :: META_KEY, $ _POST) &&
            current_user_can ('publish_posts', $ post) &&
            wp_verify_nonce ($ _ POST (self :: NONCE), self :: NONCE)
            ) {
            if ($ _POST (self :: META_KEY) === '0') {
                delete_post_meta ($ post, self :: META_KEY);
            } else {
                update_post_meta ($ post, self :: META_KEY, 1);
            }
        }
    }

}

You can then retrieve this metadata in your pages using the function get_post_meta ().

    
      
This article is sponsored