• My Custom Post Taxonomy Code

    <?php
    /* Special Offer Post */
    
    add_action('init', 'special_offers');
    function special_offers()
    {
      $labels = array(
        'name' => _x('special offers', 'post type general name'),
        'singular_name' => _x('special_offer', 'post type singular name'),
        'add_new' => _x('Add New', 'special offers'),
        'add_new_item' => __('Add New special offers'),
        'edit_item' => __('Edit special offers'),
        'new_item' => __('New special offers'),
        'view_item' => __('View special offers'),
        'search_items' => __('Search special offers'),
        'not_found' =>  __('No special_offerss found'),
        'not_found_in_trash' => __('No special offerss found in Trash'),
        'parent_item_colon' => ''
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'menu_position' => 6,
    	'_builtin' => false,
    	'menu_icon' => get_bloginfo('template_directory') . '/images/special-offer-icon.png',  // Icon Path
        'supports' => array('title','editor','thumbnail','cats' , 'brands')
      );
      register_post_type('special_offers',$args);
    }
    
    register_taxonomy( 'brands', 'special_offers', array( 'hierarchical' => true, 'label' => __('Brands'), 'query_var' => 'brands' ) );
    
    add_action( 'admin_init', 'my_admin3' );
    
    function my_admin3() {
        add_meta_box( 'special_offer_meta_box',
            'special offer Details',
            'display_special_offer_meta_box',
            'special_offer', 'normal', 'high'
        );
    }
    
    ?>
    
    <?php
    function display_special_offer_meta_box( $special_offer ) {
    
        $post_description = esc_html( get_post_meta( $special_offer->ID, 'post_description', true ) );
    	$post_description2 = esc_html( get_post_meta( $special_offer->ID, 'post_description2', true ) );
        ?>
        <table>
            <tr>
                <td style="width: 100%">Post Description[For Home Page]</td>
                <td><input type="text" size="80" name="special_offer_post_description" value="<?php echo $post_description; ?>" /></td>
            </tr>
    		<tr>
                <td style="width: 100%">Post Description 2[For Home Page]</td>
                <td><input type="text" size="80" name="special_offer_post_description2" value="<?php echo $post_description2; ?>" /></td>
            </tr>
        </table>
        <?php
    }
    ?>
    <?php
    add_action( 'save_post', 'add_special_offer_fields', 10, 2 );
    
    function add_special_offer_fields( $special_offer_id, $special_offer ) {
    
        if ( $special_offer->post_type == 'special_offers' ) {
    
            if ( isset( $_POST['special_offer_post_description'] ) && $_POST['special_offer_post_description'] != '' ) {
                update_post_meta( $special_offer_id, 'post_description', $_POST['special_offer_post_description'] );
            }
             if ( isset( $_POST['special_offer_post_description2'] ) && $_POST['special_offer_post_description2'] != '' ) {
                update_post_meta( $special_offer_id, 'post_description2', $_POST['special_offer_post_description2'] );
            }
        }
    }

    Custom fields not showing in my editor

    <div class="offer-post">
    
    <?php
    $myquery['tax_query'] = array(
    'relation' => 'OR',
    array(
    'taxonomy' => 'brands',
    'terms' => array('ALL'),
    'field' => 'slug',
    ),
    array(
    'taxonomy' => 'media',
    'terms' => array('news', 'events'),
    'field' => 'slug',
    ),
    );
    query_posts($myquery);
    
    ?>
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="so-post-hom">
    <h2 class="post-title"><?php the_title();?></h2><br />
    <h3 class="post-desc"></h3>
    <div class="post-bar">
    <a href="">Go!</a>
    </div>
    </div>
    
    <?php endwhile; else: ?>
    <?php endif; ?>

    and this code working, but in another custom post

    <?php  echo esc_html( get_post_meta( get_the_ID(), 'employed_since', true ) ); ?>

    please help me to correct the code.
    i need to show the custom field(post_description)inside <h3 class=”post-desc”></h3>
    and why can’t i see the custom field in editor ?
    also if one field, what changes in the code ?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I see two problems, there could be more, but correcting these should at least let you see something.

    Your register_taxonomy() call is not part of an ‘init’ action hook function and it needs to be. It is not in any function, so it executes when the code page loads, not the right time at all.

    Your add_meta_box() call is part of a function hooked to ‘admin_init’. This is an outdated technique from prior to WP 3.0. You now must use the ‘add_meta_boxes’ action.

    You can adapt your working output code to display this meta like so:
    <h3 class="post-desc"><?php echo esc_html( get_post_meta( get_the_ID(), 'post_description', true ) ); ?></h3>

    Thread Starter Shihab Malayil

    (@shihabmalayil)

    HI bcworkz,
    thank you for your replay. can you please help me to rewrite the same code
    correctly ?. Still i can’t see the custom field in admin panel.

    Moderator bcworkz

    (@bcworkz)

    Well, I’m not sure what you tried to do, but my thoughts were to change the applicable portion of your first snippet like so:

    register_post_type('special_offers',$args);
    
      register_taxonomy( 'brands', 'special_offers', array( 'hierarchical' => true, 'label' => __('Brands'), 'query_var' => 'brands' ) );
    }
    
    add_action(  'add_meta_boxes' , 'my_admin3' );
    
    function my_admin3() {

    This is the portion right after the $labels and $args array definitions. Note how the closing ‘}’ was moved and the add_action() tag name was changed. Unfortunately, I cannot connect to my test server for a while so I cannot verify if this is the only issue and I cannot locate any other issues by testing if not. All I can do right now is stare at the code and hope something jumps out into my attention. Not a good way to debug.

    Nothing else is jumping out. I hope these two changes show some progress, sorry I cannot help more.

    Thread Starter Shihab Malayil

    (@shihabmalayil)

    Thank you bcworkz.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Why cant i see my custom field ?.’ is closed to new replies.