Saving and displaying Custom Post Type metadata
-
Hi Everyone,
I’m having trouble saving and using metadata from a custom post type. When I update the post, the field shows up blank. I’d like this metadata to be saved and used as a link on the front page.
Here’s my custom post type file, which includes the code for adding the meta box and saving it:
<?php /** * @package Hot_Stuff */ /* Plugin Name: Hot Stuff Description: Enter and present hot products Author: Backstage Interactive Author URI: https://backstageinteractive.com Version: 1.0 Text Domain: hotstuff */ // Register Custom Post Type function hot_stuff_post_type() { $labels = array( 'name' => _x( 'Products', 'Post Type General Name', 'hotstuff' ), 'singular_name' => _x( 'Product', 'Post Type Singular Name', 'hotstuff' ), 'menu_name' => __( 'Hot Stuff Products', 'hotstuff' ), 'name_admin_bar' => __( 'Hot Stuff', 'hotstuff' ), 'archives' => __( 'Product Archives', 'hotstuff' ), 'attributes' => __( 'Product Attributes', 'hotstuff' ), 'parent_item_colon' => __( 'Parent Product:', 'hotstuff' ), 'all_items' => __( 'All Products', 'hotstuff' ), 'add_new_item' => __( 'Add New Product', 'hotstuff' ), 'add_new' => __( 'Add New', 'hotstuff' ), 'new_item' => __( 'New Product', 'hotstuff' ), 'edit_item' => __( 'Edit Product', 'hotstuff' ), 'update_item' => __( 'Update Product', 'hotstuff' ), 'view_item' => __( 'View Product', 'hotstuff' ), 'view_items' => __( 'View Products', 'hotstuff' ), 'search_items' => __( 'Search Product', 'hotstuff' ), 'not_found' => __( 'Not found', 'hotstuff' ), 'not_found_in_trash' => __( 'Not found in Trash', 'hotstuff' ), 'featured_image' => __( 'Product Image', 'hotstuff' ), 'set_featured_image' => __( 'Set Product image', 'hotstuff' ), 'remove_featured_image' => __( 'Remove Product image', 'hotstuff' ), 'use_featured_image' => __( 'Use as Product image', 'hotstuff' ), 'insert_into_item' => __( 'Insert into Product', 'hotstuff' ), 'uploaded_to_this_item' => __( 'Uploaded to this Product', 'hotstuff' ), 'items_list' => __( 'Products list', 'hotstuff' ), 'items_list_navigation' => __( 'Products list navigation', 'hotstuff' ), 'filter_items_list' => __( 'Filter Products list', 'hotstuff' ), ); $rewrite = array( 'slug' => 'stuff', 'with_front' => false, 'pages' => false, 'feeds' => false, ); $args = array( 'label' => __( 'Product', 'hotstuff' ), 'description' => __( 'Add stuff to GiveMeStuff', 'hotstuff' ), 'labels' => $labels, 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'post-formats', ), 'taxonomies' => array( 'product_type', '' ), 'hierarchical' => false, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'menu_icon' => 'dashicons-cart', 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'rewrite' => $rewrite, 'capability_type' => 'page', 'show_in_rest' => true, ); register_post_type( 'hot_stuff', $args ); } add_action( 'init', 'hot_stuff_post_type', 0 ); /* Fire our meta box setup function on the post editor screen. */ add_action( 'load-post.php', 'hotstuff_post_meta_boxes_setup' ); add_action( 'load-post-new.php', 'hotstuff_post_meta_boxes_setup' ); /* Meta box setup function. */ function hotstuff_post_meta_boxes_setup() { /* Add meta boxes on the 'add_meta_boxes' hook. */ add_action( 'add_meta_boxes', 'hotstuff_add_post_meta_boxes' ); /* Save post meta on the 'save_post' hook. */ add_action( 'save_post', 'hotstuff_save_link_meta', 10, 2 ); } /* Create one or more meta boxes to be displayed on the post editor screen. */ function hotstuff_add_post_meta_boxes() { add_meta_box( 'hotstuff-link', // Unique ID esc_html__( 'Gimme Button Link', 'example'), // Title 'hotstuff_link_meta_box', // Callback function 'hot_stuff', // Admin page (or post type) 'advanced', // Context 'default' // Priority ); } /* Display the Link post meta box. */ function hotstuff_link_meta_box( $post ) { ?> <?php wp_nonce_field( basename( __FILE__ ), 'hotstuff_link_nonce' ); ?> <p> <label for="hotstuff-link"><?php _e( "Enter affiliate link", 'example' ); ?></label> <br /> <input class="widefat" type="text" name="hotstuff-link" id="hotstuff-link" value="<?php echo esc_attr( get_post_meta( $post->ID, 'hotstuff_link', true ) ); ?>" size="30" /> </p> <?php } /* Save the meta box's post metadata. */ function hotstuff_save_link_meta( $post_id, $post ) { /* Verify the nonce before proceeding. */ if ( !isset( $_POST['hotstuff_link_nonce'] ) || !wp_verify_nonce( $_POST['hotstuff_link_nonce'], basename( __FILE__ ) ) ) return $post_id; /* Get the post type object. */ $post_type = get_post_type_object( $post->post_type ); /* Check if the current user has permission to edit the post. */ if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id; /* Get the meta key. */ $meta_key = 'hotstuff_link'; /* Get the meta value of the custom field key. */ $meta_value = get_post_meta( $post_id, $meta_key, true ); /* If a new meta value was added and there was no previous value, add it. */ if ( $new_meta_value && '' == $meta_value ) add_post_meta( $post_id, $meta_key, $new_meta_value, true ); /* If the new meta value does not match the old value, update it. */ elseif ( $new_meta_value && $new_meta_value != $meta_value ) update_post_meta( $post_id, $meta_key, $new_meta_value ); /* If there is no new meta value but an old value exists, delete it. */ elseif ( '' == $new_meta_value && $meta_value ) delete_post_meta( $post_id, $meta_key, $meta_value ); } add_filter( 'template_include', 'include_template_function', 1 ); function single_product_template( $template_path ) { if ( get_post_type() == 'hot_stuff' ) { if ( is_single() ) { // checks if the file exists in the theme first, // otherwise serve the file from the plugin if ( $theme_file = locate_template( array ( 'single-product.php' ) ) ) { $template_path = $theme_file; } else { $template_path = plugin_dir_path( __FILE__ ) . '/single-product.php'; } } } return $template_path; } function product_remove_cpt_slug( $post_link, $post, $leavename ) { if ( 'hot_stuff' != $post->post_type || 'publish' != $post->post_status ) { return $post_link; } $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); return $post_link; } add_filter( 'post_type_link', 'product_remove_cpt_slug', 10, 3 ); function product_parse_request_trick( $query ) { // Only noop the main query if ( ! $query->is_main_query() ) return; // Only noop our very specific rewrite rule match if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) { return; } // 'name' will be set if post permalinks are just post_name, otherwise the page rule will match if ( ! empty( $query->query['name'] ) ) { $query->set( 'post_type', array( 'post', 'page', 'hot_stuff' ) ); } } add_action( 'pre_get_posts', 'product_parse_request_trick' );
Here’s the code from my content file. The idea is to use the url that gets entered in the meta box as a link:
<a href="<?php echo $hotstuff_link; ?>" class="gimme-button">Gimme</a>
Any help would be greatly appreciated.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Saving and displaying Custom Post Type metadata’ is closed to new replies.