admin管理员组文章数量:1193817
I'm trying to get all posts from a particular custom post type using the following code:
$auctions = get_posts(array('post_type' => 'auction'));
print_r($auctions);
echo '<select>';
foreach ($auctions as $auction) {
echo '<option value="' . $auction->ID . '">' . $auction->post_title . '</option>';
}
echo '</select>';
Although the print_r() call shows data, the foreach seems to ignore it and doesn't print anything. Any ideas?
Any help would be appreciated
print_r() output:
Array (
[0] => WP_Post Object (
[ID] => 36
[post_author] => 1
[post_date] => 2013-05-19 10:58:45
[post_date_gmt] => 2013-05-19 08:58:45
[post_content] =>
[post_title] => My Title
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => my-title
[to_ping] =>
[pinged] =>
[post_modified] => 2013-05-24 09:55:53
[post_modified_gmt] => 2013-05-24 07:55:53
[post_content_filtered] =>
[post_parent] => 0
[guid] => /?post_type=auction&p=36
[menu_order] => 0
[post_type] => auction
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
I'm trying to get all posts from a particular custom post type using the following code:
$auctions = get_posts(array('post_type' => 'auction'));
print_r($auctions);
echo '<select>';
foreach ($auctions as $auction) {
echo '<option value="' . $auction->ID . '">' . $auction->post_title . '</option>';
}
echo '</select>';
Although the print_r() call shows data, the foreach seems to ignore it and doesn't print anything. Any ideas?
Any help would be appreciated
print_r() output:
Array (
[0] => WP_Post Object (
[ID] => 36
[post_author] => 1
[post_date] => 2013-05-19 10:58:45
[post_date_gmt] => 2013-05-19 08:58:45
[post_content] =>
[post_title] => My Title
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => my-title
[to_ping] =>
[pinged] =>
[post_modified] => 2013-05-24 09:55:53
[post_modified_gmt] => 2013-05-24 07:55:53
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://domain.com/?post_type=auction&p=36
[menu_order] => 0
[post_type] => auction
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
Share
Improve this question
edited May 28, 2013 at 20:51
Pat J
12.3k2 gold badges28 silver badges36 bronze badges
asked May 28, 2013 at 20:28
leemonleemon
2,0124 gold badges23 silver badges51 bronze badges
5
|
4 Answers
Reset to default 14You can use wp_query()
for this to work
$args = array(
'post_type' => 'auction',
'posts_per_page' => -1
);
$query = new WP_Query($args);
if ($query->have_posts()):
echo '<select>';
while ($query->have_posts()): $query->the_post();
echo '<option value="' . get_the_ID() . '">' . get_the_title() . '</option>';
endwhile;
echo '</select>';
wp_reset_postdata();
endif;
Documentation for WP_Query
https://codex.wordpress.org/Class_Reference/WP_Query
Perhaps because get_posts
returns an object, you need to setup post data per Codex get_posts. Replacing line 4:
foreach($auctions as $auction) : setup_postdata($auction) {
Try it without get_posts(). I currently have a similar function that works like this:
$args = array( 'post_type' => 'customPostName', 'post_status' => 'publish');
$pages = get_pages($args);
foreach ( $pages as $page ) {
// Do something
}
Edit: Actually I'm not sure why this isn't working since codex clearly says to use echo $post->ID;
with get_posts. http://codex.wordpress.org/Function_Reference/get_posts#Access_all_post_data
Does this make any difference for you?
foreach ($auctions as $auction) {
$option = '<option value="';
$option .= $auction->ID;
$option .= '">';
$option .= $auction->post_title;
$option .= '</option>';
echo $option;
'post_type' => 'auction',
'posts_per_page' => -1,
'post_status' => 'publish',
);
$query = new WP_Query($args);
if ($query->have_posts() ) :
echo '<select>';
while ( $query->have_posts() ) : $query->the_post();
echo '<option value="' . get_the_ID() . '">' . get_the_title() . '</option>';
endwhile;
echo '</select>';
wp_reset_postdata();
endif;
@gregory was actually right, but had a few typos... There was a closing ;
missing from the array and at the end reset_postdata();
has to become wp_reset_postdata();
.
This should work just fine now... It works great for me, without any problem!
Hope this will help!
本文标签: get all posts from a custom post type
版权声明:本文标题:get all posts from a custom post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738435293a2086640.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
print_r( $auctions );
output? (Add it to your question; it'll be very hard to read in the comments.) – Pat J Commented May 28, 2013 at 20:38<select>...</select>
statement show up in the generated HTML? If you view the source of your page, does it look correct? – Pat J Commented May 28, 2013 at 20:51