• Hi this is CPT with custom fields.

    
    
    <?php
    add_action('init', 'wlg_cstm_register');  
    
     function wlg_cstm_register() {  
    
    	$product_labels = array(
        'name' => _x('Products', 'post type general name'),
        'singular_name' => _x('product', 'post type singular name'),
        'add_new' => _x('Add New', 'product'),
        'add_new_item' => __('Add New Products'),
        'edit_item' => __('Edit Products'),
        'new_item' => __('New product'),
        'view_item' => __('View Products'),
        'search_items' => __('Search Products'),
        'not_found' =>  __('No Products found'),
        'not_found_in_trash' => __('No Products found in Trash'),
        'parent_item_colon' => ''
    );
    
    	$product_args = array(
             'labels' => $product_labels,
             'public' => true,
             'show_ui' => true,
             'capability_type' => 'post',
             'hierarchical' => false,
             'rewrite' => true,
             'supports' => array('title', 'editor', 'thumbnail'),
    		'taxonomies' => array( '' ),
            'menu_icon' => get_bloginfo('template_directory') . '/images/products-icon.png',  // Icon Path
            'has_archive' => true
            );
    
         register_post_type( 'product' , $product_args );
    	 register_taxonomy( 'brands', 'product', array( 'hierarchical' => true, 'label' => __('Product Brands'), 'query_var' => 'brands' ) );
     }
    
     add_action("admin_init", "admin_init");
    add_action('save_post', 'save_points', 1, 2);
    function admin_init(){
      add_meta_box("productInfo-meta", "Product Details", "product_meta_options", 'product', "normal", "high");
    }
    
    function product_meta_options(){
      global $post;
      $custom = get_post_custom($post->ID);
      $category = (!empty($custom["_category"][0])) ? $custom["_category"][0] : '';
      $brand = (!empty($custom["_brand"][0])) ? $custom["_brand"][0]  : '';
      $id = (!empty($custom["_productid"][0])) ? $custom["_productid"][0] : '';
      $features = (!empty($custom["_features"][0])) ? $custom["_features"][0]  : '';
      $holds = (!empty($custom["_holds"][0])) ? $custom["_holds"][0]  : '';
    ?>
    <table>
        <tr>
            <td>Category</td>
            <td> <input type="text" size="100" name="category" value="<?php echo $category; ?>" /> </td>
        </tr>
        <tr>
            <td>Brand</td>
            <td> <input type="text" size="100" name="brand" value="<?php echo $brand; ?>" /> </td>
        </tr>
    	<tr>
            <td>Product ID</td>
            <td> <input type="text" size="100" name="productid" value="<?php echo $productid; ?>" /> </td>
        </tr>
        <tr>
            <td>Features</td>
            <td><?php wp_editor( $features, 'features', $settings = array('textarea_rows'=>20) ); ?></td>
        </tr>
        <tr>
            <td>Holds</td>
            <td><?php wp_editor( $holds, 'holds', $settings = array('textarea_rows'=>20) ); ?></td>
        </tr>
    </table>
    
    <?php
    }
    
    function save_points($postid,$post){
      global $_POST;
      // set the ID to the parent post, not the revision
      $postid = (wp_is_post_revision( $postid )) ? wp_is_post_revision( $post ) : $postid;
      $post_type = get_post_type( $postid );
      if ('product' == $post_type) {
        update_post_meta($postid, "_category", $_POST["category"]);
        update_post_meta($postid, "_brand", $_POST["brand"]);
    	update_post_meta($postid, "_productid", $_POST["productid"]);
        update_post_meta($postid, "_features", $_POST["features"]);
        update_post_meta($postid, "_holds", $_POST["holds"]); // save the data
      }
    }
    
    function todo_restrict_manage_posts() {
        global $typenow;
        $args=array( 'public' => true, '_builtin' => false );
        $post_types = get_post_types($args);
        if ( in_array($typenow, $post_types) ) {
        $filters = get_object_taxonomies($typenow);
            foreach ($filters as $tax_slug) {
                $tax_obj = get_taxonomy($tax_slug);
                wp_dropdown_categories(array(
                    'show_option_all' => __('Show All '.$tax_obj->label ),
                    'taxonomy' => $tax_slug,
                    'name' => $tax_obj->name,
                    'orderby' => 'term_order',
                    'selected' => $_GET[$tax_obj->query_var],
                    'hierarchical' => $tax_obj->hierarchical,
                    'show_count' => false,
                    'hide_empty' => true
                ));
            }
        }
    }
    function todo_convert_restrict($query) {
        global $pagenow;
        global $typenow;
        if ($pagenow=='edit.php') {
            $filters = get_object_taxonomies($typenow);
            foreach ($filters as $tax_slug) {
                $var = &$query->query_vars[$tax_slug];
                if ( isset($var) ) {
                    $term = get_term_by('id',$var,$tax_slug);
                    $var = $term->slug;
                }
            }
        }
        return $query;
    }
    add_action( 'restrict_manage_posts', 'todo_restrict_manage_posts' );
    add_filter('parse_query','todo_convert_restrict');
    
    add_filter( 'manage_edit-product_columns', 'my_columns' );
    function my_columns( $columns ) {
        $columns['brands'] = 'Brands';
        $columns['category'] = 'Category';
        unset( $columns['comments'] );
        return $columns;
    }
    add_action( 'manage_posts_custom_column', 'populate_columns' );
    function populate_columns( $column ) {
        if ( 'brands' == $column ) {
            $brand = esc_html( get_post_meta( get_the_ID(), 'brand', true ) );
            echo $brand;
        }
        elseif ( 'category' == $column ) {
            $category = get_post_meta( get_the_ID(), 'category', true );
            echo $category;
        }
    }
    
    

    page template code

    
    
    <?php
    $wp_querty = new WP_Query( array(
    	'post_type'    => 'product', 'post_status'  => 'publish') ); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <?php the_post_thumbnail(); ?>
    <?php echo esc_html( get_post_meta( get_the_ID(), 'category', true ) ); ?>
    <?php echo esc_html( get_post_meta( get_the_ID(), 'brand', true ) ); ?>
    <?php echo esc_html( get_post_meta( get_the_ID(), 'productid', true ) ); ?>
    <?php the_content();?>
    <?php echo apply_filters('the_content', get_post_meta($post->ID, 'features', true)); ?>
    <?php echo get_post_meta( $post->ID, 'holds', true ); ?>
    
    <?php endwhile; else: ?>
    <?php endif; ?>
    

    here productid field not saving data others saving correctly. and all custom fields not showing content in the page. the editor and title only showing content.
    please help me to clear this issue.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Hello Shihab, sorry to see you hare having other troubles. I think productid is saving, but the saved value does not get populated on the form because the saved value is assigned to $id, but the form displays the value of $productid, which is undefined at that point. Change the assignment of $id to $productid in the 5th line of the product_meta_options() function. The form will then display the saved value.

    The reason the template does not display values is you confused form field names and meta names. All meta names have a preceding underscore as in ‘_category’, corresponding to a form field name of ‘category’. If you change all the meta names in calls to get_post_meta() on the template to be preceded with an underscore (i.e. change ‘category’ to ‘_category’ and similar for each meta name) you should then see the saved values.

Viewing 1 replies (of 1 total)
  • The topic ‘what error in my CPT and Template ?’ is closed to new replies.