如何按日期订购 posts - 从 wp_query 和自定义字段中提取 post ID
How to order posts by date - pulling post ID from wp_query and custom field
我正在使用 Custom Field Suite 插件向我的页面添加帖子。在我的页面上,使用下面的代码,帖子按标题的字母顺序显示(这是因为在页面编辑器的小部件中,它们默认按字母顺序排列,但也可以手动移动)。
但是,我希望它们按发布日期显示。
这是我正在使用的自定义字段套件字段类型:http://customfieldsuite.com/field-types/relationship.html
我的代码
// The Query
$archive = CFS()->get('archive'); //getting posts from widget
foreach ( $archive as $post_id ) {
$the_post1 = get_post( $post_id );
$the_post = $the_post1->ID;
$the_query2 = new WP_Query( array(
'p' => $the_post,
'orderby' => 'date', //this isn't working
'order' => 'ASC'
));
// The Loop
if ( $the_query2->have_posts() ) {
echo '<ul>';
while ( $the_query2->have_posts() ) {
$the_query2->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
}
您可以将所有 ID 存储在一个数组中,然后在您的 WP_Query 中搜索它们,并且您可以节省一些 space 而无需声明 $the_post1 和 $the_post, 因为你已经有了你的 post id.
$archive = CFS()->get('archive'); //getting posts from widget
$posts = new array();
foreach ( $archive as $post_id ) {
array_push($posts, $post_id);
}
$the_query2 = new WP_Query( array(
'post_type' => 'post',
'post__in' => $posts,
'orderby' => 'date', //this isn't working
'order' => 'ASC'
));
// The Loop
if ( $the_query2->have_posts() ) {
echo '<ul>';
while ( $the_query2->have_posts() ) {
$the_query2->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
未来WP_Query参考:https://codex.wordpress.org/Class_Reference/WP_Query
我正在使用 Custom Field Suite 插件向我的页面添加帖子。在我的页面上,使用下面的代码,帖子按标题的字母顺序显示(这是因为在页面编辑器的小部件中,它们默认按字母顺序排列,但也可以手动移动)。
但是,我希望它们按发布日期显示。
这是我正在使用的自定义字段套件字段类型:http://customfieldsuite.com/field-types/relationship.html
我的代码
// The Query
$archive = CFS()->get('archive'); //getting posts from widget
foreach ( $archive as $post_id ) {
$the_post1 = get_post( $post_id );
$the_post = $the_post1->ID;
$the_query2 = new WP_Query( array(
'p' => $the_post,
'orderby' => 'date', //this isn't working
'order' => 'ASC'
));
// The Loop
if ( $the_query2->have_posts() ) {
echo '<ul>';
while ( $the_query2->have_posts() ) {
$the_query2->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
}
您可以将所有 ID 存储在一个数组中,然后在您的 WP_Query 中搜索它们,并且您可以节省一些 space 而无需声明 $the_post1 和 $the_post, 因为你已经有了你的 post id.
$archive = CFS()->get('archive'); //getting posts from widget
$posts = new array();
foreach ( $archive as $post_id ) {
array_push($posts, $post_id);
}
$the_query2 = new WP_Query( array(
'post_type' => 'post',
'post__in' => $posts,
'orderby' => 'date', //this isn't working
'order' => 'ASC'
));
// The Loop
if ( $the_query2->have_posts() ) {
echo '<ul>';
while ( $the_query2->have_posts() ) {
$the_query2->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
未来WP_Query参考:https://codex.wordpress.org/Class_Reference/WP_Query