Featured Post with meta in custom post type

/* custom post type trove */

function trove_custom_init() {
$labels = array(
'name' => __('Treasure Trove','themename'),
'singular_name' => __('trove','themename'),
'add_new' => __('Add New','themename'),
'add_new_item' => __('Add New Treasure Trove','themename'),
'edit_item' => __('Edit Treasure Trove','themename'),
'new_item' => __('New Treasure Trove','themename'),
'all_items' => __('All Treasure Trove','themename'),
'view_item' => __('View Treasure Trove','themename'),
'search_items' => __('Search Treasure Trove','themename'),
'not_found' => __('No Treasure Trove found','themename'),
'not_found_in_trash' => __('No Treasure Trove found in Trash','themename'),
'parent_item_colon' => "",
'menu_name' => __('Treasure Trove','themename')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'trove' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'menu_icon'   => 'dashicons-sos',
'supports' => array( 'title', 'editor','thumbnail','page-attributes'),             
);
register_post_type( 'trove', $args );

/*metabox*/
add_action( 'add_meta_boxes', 'meta_box_trove' );
function meta_box_trove()
{
add_meta_box( 'meta_box_trove_id', 'Treasure Trove info', 'trove_info', 'trove', 'normal', 'high' );
}



function trove_info( $post )
{
$values = get_post_custom( $post->ID );
$fragrances = isset( $values['fragrances'] ) ? esc_attr( $values['fragrances'][0] ) : "";
$house = isset( $values['house'] ) ? esc_attr( $values['house'][0] ) : "";
$size = isset( $values['size'] ) ? esc_attr( $values['size'][0] ) : "";
$stregenth = isset( $values['stregenth'] ) ? esc_attr( $values['stregenth'][0] ) : "";
$orn = isset( $values['orn'] ) ? esc_attr( $values['orn'][0] ) : "";
$price = isset( $values['price'] ) ? esc_attr( $values['price'][0] ) : "";

wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<input type="checkbox" name="featured_post" <?php if( get_post_meta($post->ID,'featured_post',true) == true ) { ?>checked="checked"<?php } ?> />  Set Featured Post.
<p>FRAGRANCE:</p>
<input type="text" name="fragrances" value="<?php echo esc_attr( $fragrances ); ?>" class="widefat" placeholder="fragrances" />
<p>HOUSE:</p>
<input type="text" name="house" value="<?php echo esc_attr( $house ); ?>" class="widefat" placeholder="house" />
<p>SIZE:</p>
<input type="text" name="size" value="<?php echo esc_attr( $size ); ?>" class="widefat" placeholder="size" />
<p>STRENGTH:</p>
<input type="text" name="stregenth" value="<?php echo esc_attr( $stregenth ); ?>" class="widefat" placeholder="stregenth" />
<p>Order Ref No:</p>
<input type="text" name="orn" value="<?php echo esc_attr( $orn ); ?>" class="widefat" placeholder="Order Ref No" />
<p>PRICE:</p>
<input type="text" name="price" value="<?php echo esc_attr( $price ); ?>" class="widefat" placeholder="price" />

<?php
}
/*save metabox*/
add_action( 'save_post', 'trove_meta_box_save' );
function trove_meta_box_save( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( isset( $_POST['featured_post'] ) ) 
{
	update_post_meta( $post_id, 'featured_post', true );
}
else
{
	update_post_meta( $post_id, 'featured_post', false );
}
if( isset( $_POST['fragrances'] ) ) update_post_meta( $post_id, 'fragrances', esc_attr( $_POST['fragrances'] ) );
if( isset( $_POST['house'] ) ) update_post_meta( $post_id, 'house', esc_attr( $_POST['house'] ) );
if( isset( $_POST['size'] ) ) update_post_meta( $post_id, 'size', esc_attr( $_POST['size'] ) );
if( isset( $_POST['orn'] ) ) update_post_meta( $post_id, 'orn', esc_attr( $_POST['orn'] ) );
if( isset( $_POST['stregenth'] ) ) update_post_meta( $post_id, 'stregenth', esc_attr( $_POST['stregenth'] ) );
if( isset( $_POST['price'] ) ) update_post_meta( $post_id, 'price', esc_attr( $_POST['price'] ) );
}
}
add_action( 'init', 'trove_custom_init' );

Display


$featured_post_args = array(
					'post_type' => 'trove',
					'posts_per_page' => -1,
					'meta_value' => 1,
					'meta_key' => 'featured_post'
		);
			
		$featured_post_query = new WP_Query($featured_post_args);
		
		while($featured_post_query->have_posts()):$featured_post_query->the_post();
			echo '<div class="advertisement">';
				?><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a><?php
			echo '</div>';
		endwhile;

Advertisements

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: