• hardik

    (@hardikp)


    Hi,
    I want to add post meta for custom post type “ad”.
    Here is the code I am using to add post meta.
    I have put this code in my theme’s functions.php file but still post meta not showing in any ad from wordpress admin.


    function add_ads_shared( $post_id ) {
    add_post_meta( $post_id, 'et_ad_shared', 0, true);
    }
    add_action( 'ad', 'add_ads_shared' );

Viewing 2 replies - 1 through 2 (of 2 total)
  • a2ztechnologies

    (@a2ztechnologies)

    Try this code

    //custom Meta Box
    add_action( 'admin_init', 'my_admin_custom' );
    function my_admin_custom() {
    	add_meta_box( 'custom_meta_box', 'Car Details', 'display_custom_meta_box','custom', 'normal', 'high' );
    }
    function display_custom_meta_box( $custom ) {
    	?>
    	<h4>General Details</h4>
    	<table width="100%">
            <tr>
                <td style="width: 25%">Meta Label Name</td>
                <td><input type="text" style="width:425px;" name="meta[meta_1]" value="<?php echo esc_html( get_post_meta( $custom->ID, 'meta_1', true ) );?>" />
                </td>
    		</tr>
    	</table>
    <?php
    }
    add_action( 'save_post', 'add_custom_fields', 10, 2 );
    function add_custom_fields( $custom_id, $custom ) {
    	if ( $custom->post_type == 'custom' ) {
    		if ( isset( $_POST['meta'] ) ) {
    			foreach( $_POST['meta'] as $key => $value ){
    				update_post_meta( $custom_id, $key, $value );
    			}
    		}
    	}
    }
    Thread Starter hardik

    (@hardikp)

    It is not working for me.

    Actually I want to add post meta in custom field not to add post meta box.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to add post meta in custom post type?’ is closed to new replies.