Php 如何在WordPress的查询中同时按两种不同的东西排序
我有点小问题。我正试图通过一个自定义元字段获取最新的12篇文章。在这12篇文章中,我想按投递日期排序。因此,首先我使用一个自定义元字段取出12篇文章,并对它们进行排序,以便通过元字段查找最新的文章。一旦我有了它们,我想用最新的帖子重新订购它们 这是我当前的代码,我不知道如何在一个查询中输入两个order BYPhp 如何在WordPress的查询中同时按两种不同的东西排序,php,wordpress,Php,Wordpress,我有点小问题。我正试图通过一个自定义元字段获取最新的12篇文章。在这12篇文章中,我想按投递日期排序。因此,首先我使用一个自定义元字段取出12篇文章,并对它们进行排序,以便通过元字段查找最新的文章。一旦我有了它们,我想用最新的帖子重新订购它们 这是我当前的代码,我不知道如何在一个查询中输入两个order BY $recentEpisodes12 = new WP_Query(array( 'posts_per_page' => 12, 'post_type' => '
$recentEpisodes12 = new WP_Query(array(
'posts_per_page' => 12,
'post_type' => 'post',
'meta_key' => 'air_date',
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_query' => array(
array(
'key' => 'air_date',
),
array(
'key' => 'already_aired',
'value' => 'yes',
'compare' => '='
)
),
));
,您只需将它们用空格隔开:
多个“orderby”值
显示按“标题”和“菜单顺序”排序的页面。(标题为
(主要):
在您的情况下,这看起来像:
'orderby' => 'meta_value_num date'
编辑:好的,看起来您正在尝试做一些更复杂的事情。我是这样理解的,如果我错了,请纠正我的错误:
air\u日期
仅保留12件最新物品日期
对结果12项进行排序orderby
所做的是:
air\u date
值,请按date
订购air\u date
进行区分,而orderby
的正常用法使用这两个标准来确定结果中的最终项目
不过,我不知道一个简单的方法来解决这个问题。但是,由于您只想更改结果项的显示顺序,因此您可以自己对它们进行排序。您可以使用get_posts
而不是WP_Query
,并使用PHP的
所以,如果您想按发布日期排序,为什么需要一个元字段呢?一旦你通过meta值获得了12篇最新的文章,那么
air\u日期是否与文章日期有所不同
值得注意的是:posts\u per\u page
并不限制返回的总数。它只是告诉WordPress何时拆分为新页面
类似的东西应该是基于你在原始帖子中描述的
// Post Criteria
$post_criteria = array(
'posts_per_page' => 12, // Don't paginate until after 12 posts
'orderby' => 'post_date', // Order by post date
'order' => 'DESC', // Order them reverse chronologically (ie. get the newest first)
'meta_key' => 'air_date', // Only get posts with this meta key
'meta_value' => 'meta_value_num', // And this meta value (only needed if you have multiple possible values for the meta key and just want one)
'post_type' => 'post', // Only get posts
'post_status' => 'publish'); // Only get posts that are published (ie. no drafts, etc.)
// Get posts matching criteria
$posts = get_posts( $post_criteria );
// Discard posts after the 12th one
$posts = array_slice($posts, 0, 12);
// If you wanted to reverse them to display
// chronologically, uncomment this variable.
# $posts = array_reverse($posts);
// For each post
foreach ($posts as $post) {
// Basic variables
// See a full list here: http://codex.wordpress.org/Function_Reference/get_post#Return
$id = $post->ID;
$title = $post->post_title;
$url = get_permalink($id);
$content = $post->post_content;
// Do stuff with the posts here.
}
在WordPress4.2及更高版本中,通过一个或多个自定义字段进行排序变得更加容易。有关示例,请参见此链接:
通过向orderby now传递数组,您甚至可以对一列ASC和另一列DESC进行排序:
'orderby' => array(
'city_clause' => 'ASC',
'state_clause' => 'DESC',
),
你似乎有正确的答案,但它给了我意想不到的结果。而不是接近我试图实现的x.x'orderby'=>'date meta\u value\u num',
。。。甚至试图改变顺序,因为先到的优先。是的,这基本上就是我想做的。我是否需要在php中为usort函数使用get_posts?另外,一旦我从air_date获得12个项目,我将如何比较发布日期。我对PHP不太在行。@user27899我用一个简单的排序回调更新了我的答案。当我不想弄乱“主循环”时,我更喜欢get_posts
。你可以对query\u posts
做同样的操作,然后你必须在检索完你的帖子后立即对$wp\u query->posts进行排序。太棒了!工作得很好!获取按meta_value_num排序的12篇文章。。。航空日期和邮递日期有很大不同。一旦我按meta_value_num得到12篇文章,然后我就按发布日期对它们进行排序。
// Post Criteria
$post_criteria = array(
'posts_per_page' => 12, // Don't paginate until after 12 posts
'orderby' => 'post_date', // Order by post date
'order' => 'DESC', // Order them reverse chronologically (ie. get the newest first)
'meta_key' => 'air_date', // Only get posts with this meta key
'meta_value' => 'meta_value_num', // And this meta value (only needed if you have multiple possible values for the meta key and just want one)
'post_type' => 'post', // Only get posts
'post_status' => 'publish'); // Only get posts that are published (ie. no drafts, etc.)
// Get posts matching criteria
$posts = get_posts( $post_criteria );
// Discard posts after the 12th one
$posts = array_slice($posts, 0, 12);
// If you wanted to reverse them to display
// chronologically, uncomment this variable.
# $posts = array_reverse($posts);
// For each post
foreach ($posts as $post) {
// Basic variables
// See a full list here: http://codex.wordpress.org/Function_Reference/get_post#Return
$id = $post->ID;
$title = $post->post_title;
$url = get_permalink($id);
$content = $post->post_content;
// Do stuff with the posts here.
}
'orderby' => array(
'city_clause' => 'ASC',
'state_clause' => 'DESC',
),