I would like to generate a drop down of posts, but also include in that list my custom post types.
I figured that wp_get_archives was the best for this as wp_dropdown_posts does not exist.
<select name=\"archive-dropdown\">
<option value=\"\"><?php echo attribute_escape(__('Article')); ?></option>
<?php wp_get_archives('type=postbypost&format=option&show_post_count=0'); ?>
</select>
My custom post types are:
video
reading
<strong>Edit</strong>
Sorry for not including this, but I also need to group the output by post_type under their post_type heading?
eg:
Videos
- Video post
Reading
- Reading post
etc
Arnav Joy answers:
try this in functions.php
<?php
add_filter( 'getarchives_where' , 'ucc_getarchives_where_filter' , 10 , 2 );
function ucc_getarchives_where_filter( $where , $r ) {
$args = array( 'public' => true , '_builtin' => false );
$output = 'names'; $operator = 'and';
$post_types = get_post_types( $args , $output , $operator );
$post_types = array_merge( $post_types , array( 'post' ,'video' , 'reading' ) );
$post_types = "'" . implode( "' , '" , $post_types ) . "'";
return str_replace( "post_type = 'post'" , "post_type IN ( $post_types )" , $where );
}
?>
Nick comments:
Thanks Arnav, that almost worked
How would I group them by their post_type under their post_type heading?
eg:
Videos
- Video post
Reading
- Reading post
etc
Nick comments:
Hi Arnav.
Also this code seems to show all posts.
So the list is showing all posts from all my custom post types, not just 'reading, video' and posts.
Thanks
Arnav Joy comments:
replace the line
$post_types = array_merge( $post_types , array( 'post' ,'video' , 'reading' ) );
with
$post_types = array( 'post' ,'video' , 'reading' ) ;
and try then
Luis Abarca answers:
wp_dropdown_pages with post_type parameter maybe
[[LINK href="http://codex.wordpress.org/Function_Reference/wp_dropdown_pages#Other_Parameters"]]http://codex.wordpress.org/Function_Reference/wp_dropdown_pages#Other_Parameters[[/LINK]]
Luis Abarca comments:
With a custom code
$id = $name = 'post_custom_select';
$posts = get_posts();
$custom = get_posts('post_type=your_custom_post_type');
$myposts = array_merge($posts, $custom);
if ( ! empty($myposts) ) {
$output = "<select name='" . esc_attr( $name ) . "' id='" . esc_attr( $id ) . "'>\n";
foreach( $myposts as $post ) {
setup_postdata($post);
$output .= '<option value="' . the_ID() . '">' . the_title() . '</option>";
}
$output .= "</select>\n";
}
echo $output;
Luis Abarca comments:
Try this quick solution
function select_builder($cpt, $label)
{
$myposts = get_posts('post_type=' . $cpt);
$output .= '<optgroup label="' . $label . '">';
foreach( $myposts as $post ) {
setup_postdata($post);
$output .= '<option value="' . the_ID() . '">' . the_title() . '</option>";
}
return $output .= '</optgroup>';
}
$id = $name = 'post_custom_select';
$data = array(
'posts' => 'Posts',
'video' => 'Videos',
'reading' => 'Reading'
);
if ( ! empty($myposts) ) {
$output = "<select name='" . esc_attr( $name ) . "' id='" . esc_attr( $id ) . "'>\n";
foreach ($data as $cpt => $label) {
$output .= select_builder($cpt, $label);
}
$output .= "</select>\n";
}
echo $output;
Navjot Singh answers:
You can add custom post types to wp_get_archives function using code from [[LINK href="http://bajada.net/2010/07/15/adding-custom-post-types-to-wp_get_archives"]]here[[/LINK]].