Juliver Galleto
Juliver Galleto

Reputation: 5

displaying the post type base on the post meta wordpress

I have this code for a custom post type and a post meta

add_action( 'init', 'create_sidebar_picture' );

function create_sidebar_picture() {
register_post_type( 'sidebar_picture',
array(
'labels' => array(
'name' => 'Sidebar Pictures',
'singular_name' => 'Sidebar Pictures',
'add_new' => 'Add New',
'add_new_item' => 'Add New Sidebar Picture',
'edit' => 'Edit',
'edit_item' => 'Edit Sidebar Picture',
'new_item' => 'New Sidebar Picture',
'view' => 'View Sidebar Picture',
'view_item' => 'View ',
'search_items' => 'Search Sidebar Picture',
'not_found' => 'No Sidebar Picture found',
'not_found_in_trash' =>
'No Sidebar Picture found in Trash',
'parent' => 'Parent Sidebar Picture'
),
'public' => true,
'menu_position' => 15,
'supports' =>
array( 'title', 'editor', 'comments',
'thumbnail',  ),
'taxonomies' => array( '' ),
'menu_icon' => get_template_directory_uri() . '/images/ggicon.png',
'has_archive' => true
)
);
}

add_action( 'admin_init', 'my_admin' );

function my_admin() {
add_meta_box( 'sidebar_picture_meta_box',
'Sidebar Picture Details',
'display_sidebar_picture_meta_box',
'sidebar_picture', 'normal', 'high' );
}

function display_sidebar_picture_meta_box( $sidebar_picture ) {
// Retrieve current name of the Director and Movie Rating based on review ID
$name_picture = esc_html( get_post_meta( $sidebar_picture->ID, 'name_picture', true ) );
$sidebar_group = intval( get_post_meta( $sidebar_picture->ID,'sidebar_group', true ) );
?>
<table>
<tr>
<td style="width: 100%">Picture Name</td>
<td><input type="text" size="80"
name="sidebar_picture_name_picture"
value="<?php echo $name_picture; ?>" /></td>
</tr>
<tr>
<td style="width: 150px">Which Sidebar You Want This To Be Displayed?</td>
<td>
<select style="width: 100px"
name="sidebar_picture_sidebar_group">
<?php
// Generate all items of drop-down list
for ( $rating = 2; $rating >= 1; $rating -- ) {
?>
<option value="<?php echo $rating; ?>"
<?php echo selected( $rating,
$sidebar_group ); ?>>
<?php echo $rating; ?> stars
<?php } ?>
</select>
</td>
</tr>
</table>
<?php }


add_action( 'save_post', 'add_sidebar_picture_fields', 10, 2 );


function add_sidebar_picture_fields( $sidebar_picture_id, $sidebar_picture ) {
// Check post type for movie reviews
if ( $sidebar_picture->post_type == 'sidebar_picture' ) {
// Store data in post meta table if present in post data
if ( isset( $_POST['sidebar_picture_name_picture'] ) &&
$_POST['sidebar_picture_name_picture'] != '' ) {
update_post_meta( $sidebar_picture_id, 'name_picture',
$_POST['sidebar_picture_name_picture'] );
}
if ( isset( $_POST['sidebar_picture_sidebar_group'] ) &&
$_POST['sidebar_picture_sidebar_group'] != '' ) {
update_post_meta( $sidebar_picture_id, 'sidebar_group',
$_POST['sidebar_picture_sidebar_group'] );
}
}
}

as you can see from above codes, it compose of post type with a post meta. Now, I have this code (please see below) which displayed the post type sidebar picture.

<?php query_posts(array('post_type'=>'sidebar_picture')); ?>
<?php $mypost = array( 'post_type' => 'sidebar_picture' );
$loop = new WP_Query( $mypost ); ?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<?php endwhile; ?>

and the above code is to display the post type. Now, what i want is to display all the post type that has a post meta sidebar group of 1, does anyone here could give me an idea how to make it? currently im searching on the net for a solution but so far, found nothing good.

Im open in any suggestion, recommendations and idea's. Thank you in advance.

Upvotes: 0

Views: 88

Answers (1)

diggy
diggy

Reputation: 6828

Use the meta_query arg:

$loop = new WP_Query( array(
    'post_type' => 'sidebar_picture',
    'meta_query' => array(
        array(
            'key' => 'sidebar_group',
            'value' => '1'
        )
    )
) );

More info: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

Upvotes: 1

Related Questions