Wordpress 循环:自定义字段值“是” >按分类法“a”排序;和自定义字段值“否”; >按分类法排序 'b'

发布于 2025-01-15 13:50:59 字数 874 浏览 1 评论 0原文

现在尝试搜索和测试两天,但似乎没有让这个循环按照我想要的方式工作。任何帮助将不胜感激。

  1. 我想循环遍历自定义帖子类型(我知道如何执行此操作)
  2. 在此循环中,我首先想列出一些具有自定义字段值(“是”)的帖子(数量可能会有所不同),然后对这些帖子进行排序基于分类值(长度)的帖子。
  3. 然后我想列出其余的帖子(这些帖子有一个自定义字段值“否”)并根据分类值(年份)对这些帖子进行排序。

这些页面使用分页。我尝试了两个不同的循环,但由于第一部分是未定义数量的帖子,这对我不起作用。最重要的是,如果可能的话,它在一个循环中感觉“更干净”。

到目前为止,这就是我所拥有的:

<?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.

  1. I want to loop through a custom post type (I know how to do this)
  2. 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).
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白况 2025-01-22 13:50:59

您必须提出两个查询,一个针对“是”的帖子,另一个针对“否”的帖子。另外,调用元查询时您的查询是错误的。

我们设置分页:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

“是”帖子:

<?php $yes_posts = get_posts(array(
   'post_type' => 'custom-post-type-name',
   'posts_per_page' => 24,
   'paged' => $paged,
   'meta_query' => array(
     array(
         'meta_key' => 'highlighted-post',
         'meta_value' => 'yes',
       ),
   ),
   'order' => 'ASC'
)); ?>

“否”帖子:

<?php $no_posts = get_posts(array(
   'post_type' => 'custom-post-type-name',
   'posts_per_page' => 24,
   'paged' => $paged,
   'meta_query' => array(
      array(
         'meta_key' => 'highlighted-post',
         'meta_value' => 'no'
      ), 
   ),
   'orderby' => 'DESC'
)); ?>

我们组合查询以获得最后一个查询的帖子 ID 列表:

$allposts = array_merge($yes_posts, $no_posts);
$postids = array();
foreach( $allposts as $item ) {
    $postids[] = $item->ID;
}

我们创建一个帖子 ID 数组

$unique = array_unique($postids);
$args = array(
    'post__in' => $unique,
    'paged' => $paged,
    'orderby' => 'post__in'
);

我们执行循环:

$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
    $checking = 0;
    while ( $the_query->have_posts() ) { $the_query->the_post();
        // Handle posts here
    }
    wp_reset_postdata();
}

将每个代码组合在一个页面,它应该可以工作。

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:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

The 'yes' posts:

<?php $yes_posts = get_posts(array(
   'post_type' => 'custom-post-type-name',
   'posts_per_page' => 24,
   'paged' => $paged,
   'meta_query' => array(
     array(
         'meta_key' => 'highlighted-post',
         'meta_value' => 'yes',
       ),
   ),
   'order' => 'ASC'
)); ?>

The 'no' posts:

<?php $no_posts = get_posts(array(
   'post_type' => 'custom-post-type-name',
   'posts_per_page' => 24,
   'paged' => $paged,
   'meta_query' => array(
      array(
         'meta_key' => 'highlighted-post',
         'meta_value' => 'no'
      ), 
   ),
   'orderby' => 'DESC'
)); ?>

We combine the queries to get a list of post ids for the last query:

$allposts = array_merge($yes_posts, $no_posts);
$postids = array();
foreach( $allposts as $item ) {
    $postids[] = $item->ID;
}

We create an array of post IDs

$unique = array_unique($postids);
$args = array(
    'post__in' => $unique,
    'paged' => $paged,
    'orderby' => 'post__in'
);

We do the loop:

$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
    $checking = 0;
    while ( $the_query->have_posts() ) { $the_query->the_post();
        // Handle posts here
    }
    wp_reset_postdata();
}

Combine every code in a page and it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文