Wordpress 循环:自定义字段值“是” >按分类法“a”排序;和自定义字段值“否”; >按分类法排序 'b'
现在尝试搜索和测试两天,但似乎没有让这个循环按照我想要的方式工作。任何帮助将不胜感激。
- 我想循环遍历自定义帖子类型(我知道如何执行此操作)
- 在此循环中,我首先想列出一些具有自定义字段值(“是”)的帖子(数量可能会有所不同),然后对这些帖子进行排序基于分类值(长度)的帖子。
- 然后我想列出其余的帖子(这些帖子有一个自定义字段值“否”)并根据分类值(年份)对这些帖子进行排序。
这些页面使用分页。我尝试了两个不同的循环,但由于第一部分是未定义数量的帖子,这对我不起作用。最重要的是,如果可能的话,它在一个循环中感觉“更干净”。
到目前为止,这就是我所拥有的:
<?php $args = array(
'post_type' => 'custom-post-type-name',
'posts_per_page' => 24,
'paged' => $paged,
'meta_query' => array(
'first-posts' => array(
'meta_key' => 'highlighted-post',
'meta_value' => 'yes',
),
'other-posts' => array(
'meta_key' => 'highlighted-post',
'meta_value' => 'no'
),
),
'orderby' => array(
'first-posts' => 'ASC',
'other-posts' => 'DESC'
),
); ?>
Tried searching and testing for two days now, but don't seem to get this loop working in the way that I want it. Any help would be greatly appreciated.
- I want to loop through a custom post type (I know how to do this)
- In this loop, I first would like to list a few posts (number can vary) with a custom field value ('yes'), and then sort these posts based on a taxonomy value (length).
- Then I would like to list the rest of the posts (these have a custom field value 'no') and sort these posts based on a taxonomy value (year).
These pages use pagination. I tried two different loops but since the first part is an undefined number of posts, this does not work for me. And on top of that it just feels 'cleaner' in one loop if possible.
So far this is what I have:
<?php $args = array(
'post_type' => 'custom-post-type-name',
'posts_per_page' => 24,
'paged' => $paged,
'meta_query' => array(
'first-posts' => array(
'meta_key' => 'highlighted-post',
'meta_value' => 'yes',
),
'other-posts' => array(
'meta_key' => 'highlighted-post',
'meta_value' => 'no'
),
),
'orderby' => array(
'first-posts' => 'ASC',
'other-posts' => 'DESC'
),
); ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须提出两个查询,一个针对“是”的帖子,另一个针对“否”的帖子。另外,调用元查询时您的查询是错误的。
我们设置分页:
“是”帖子:
“否”帖子:
我们组合查询以获得最后一个查询的帖子 ID 列表:
我们创建一个帖子 ID 数组
我们执行循环:
将每个代码组合在一个页面,它应该可以工作。
You have to make two queries, one for the 'yes' posts and another for the 'no'. Also, your query is wrong when calling meta query.
We set the pagination:
The 'yes' posts:
The 'no' posts:
We combine the queries to get a list of post ids for the last query:
We create an array of post IDs
We do the loop:
Combine every code in a page and it should work.