Skip to content

Instantly share code, notes, and snippets.

@nacin
Created September 19, 2011 00:59
Show Gist options
  • Save nacin/1225786 to your computer and use it in GitHub Desktop.
Save nacin/1225786 to your computer and use it in GitHub Desktop.
Random Custom Post Type Example from WCPDX
<?php
add_action( 'init', 'tweets_init' );
function tweets_init() {
register_post_type( 'tweets', array(
'public' => true,
'labels' => array(
'name' => 'Tweets',
'singular_name' => 'Tweet',
),
'show_ui' => true,
'supports' => array( 'title', 'excerpt', 'editor', 'thumbnail' ),
'rewrite' => true,
'has_archive' => true,
) );
}
add_action( 'add_meta_boxes_tweets', 'tweets_add_meta_boxes' );
function tweets_add_meta_boxes( $post ) {
add_meta_box( 'tweets_meta_box', 'Shoe Size', 'tweets_meta_box', 'tweets', 'normal' );
}
function tweets_meta_box( $post ) {
$value = get_post_meta( $post->ID, '_shoe_size', true );
echo '<input type="text" name="tweets_shoe_size" value="' . esc_attr( $value ) . '" />';
}
add_action( 'save_post', 'tweets_save_post', 10, 2 );
function tweets_save_post( $post_id, $post ) {
if ( 'tweets' != $post->post_type )
return;
if ( defined( 'DOING_AJAX' ) || defined( 'DOING_AUTOSAVE' ) )
return;
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
if ( ! isset( $_POST['tweets_shoe_size'] ) )
return;
$value = floatval( $_POST['tweets_shoe_size'] );
update_post_meta( $post_id, '_shoe_size', $value );
}
/*
$query = new WP_Query( array(
'post_type' => 'tweets',
'meta_query' => array( array(
'key' => '_shoe_size',
'value' => '5',
'compare' => '>',
'type' => 'NUMERIC',
) ),
) );
var_dump( $query->posts );
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment