首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查询自定义邮政类型&按自定义邮政类型订购

查询自定义邮政类型&按自定义邮政类型订购
EN

Stack Overflow用户
提问于 2019-04-07 12:34:38
回答 3查看 758关注 0票数 2

是否可以查询多个CPT,然后以预先设置的方式进行排序?

例如,我有2个CPT&1是WP默认的'post‘,希望循环返回6个结果,排序如下。

  • CPT-1
  • CPT-2
  • 帖子
  • CPT-1
  • CPT-2
  • 帖子

如果不分裂循环,这是可能的吗?

我有一个快速的搜索,但只能找到一篇有关这方面的文章,解决方案似乎不再有效.

Wordpress order posts by post type

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-04-11 21:25:27

以下是Sephsekla代码的简写版本:

代码语言:javascript
复制
$my_post_types = array( 'CPT-1', 'CPT-2', 'post', 'CPT-1', 'CPT-2', 'post' );
$posts_shown = array();
$args = array(
    'post_type' => array( 'CPT-1', 'CPT-2', 'post' ),
    'post_status' => 'publish',
    'posts_per_page' => -1
);
$my_query = new WP_Query( $args );
foreach ( $my_post_types as $post_type ):
    while ( $my_query->have_posts() ): $my_query->the_post();
        if ( $post_type == get_post_type() && ! in_array( get_the_ID(), $posts_shown ) ) {
            echo '<pre>' . get_post_type() .': '. get_the_title() . '</pre>';
            $posts_shown[] = get_the_id();
            break;
        }
    endwhile;
    $my_query->rewind_posts();
endforeach;
wp_reset_postdata();
票数 3
EN

Stack Overflow用户

发布于 2019-04-10 13:11:51

这是我之前做过的一件事,虽然它只需要一个查询就可以完成,但这是一个冗长的设置。

它的要点是您可以使用一个查询,然后循环查询,直到找到您想要的第一个帖子。然后退出循环,并使用WP_Query->rewind_posts()将查询放回起始位置。

然后,您可以使用不同的条件运行第二个循环。然后是第三个。

对于第四、第五和第六循环,您还需要检查是否没有重复第一组。

请参阅下面代码的所有荣耀。

代码语言:javascript
复制
<?php

$my_query = new WP_Query(
    array(
        'post_status' => 'publish',
    )
);

$post_1 = $post_2 = $post_3 = $post_4 = $post_5 = $post_6 = 0;

if ( $my_query->have_posts() ) {
    /*First loop through posts*/
    while ( $my_query->have_posts() ) {
        $my_query->the_post();
        /**
         * Find the first post
        */

        if ( 'CPT-1' == get_post_type() && $post_1 == 0 ) {
            do_something_with_the_post();
            $post_1 = get_the_id();
            break;
        }
    }

    $my_query->rewind_posts();

    /*Second loop through posts*/
    while ( $my_query->have_posts() ) {
        $my_query->the_post();

        /**
         * Find the second post
        */
        if ( 'CPT-2' == get_post_type() && $post_2 == 0 ) {
            do_something_with_the_post();
            $post_2 = get_the_id();
            break;
        }
    }
    $my_query->rewind_posts();

    /*Third loop through posts*/
    while ( $my_query->have_posts() ) {
        $my_query->the_post();

        /**
         * Find the third post
        */
        if ( 'post' == get_post_type() && $post_3 == 0 ) {
            do_something_with_the_post();
            $post_3 = get_the_id();
            break;
        }
    }

    $my_query->rewind_posts();

    /**
     * Then we repeat this process but also check we don't use the same post twice
     */

    /*Fourth loop through posts*/
    while ( $my_query->have_posts() ) {
        $my_query->the_post();

        /**
         * Find the fourth post
        */
        if ( 'CPT-1' == get_post_type() && $post_4 == 0 && get_the_id() !== $post_1 ) {
            do_something_with_the_post();
            $post_1 = get_the_id();
            break;
        }
    }

    $my_query->rewind_posts();

    /*Fifth loop through posts*/
    while ( $my_query->have_posts() ) {
        $my_query->the_post();

        /**
         * Find the fifth post
        */

        if ( 'CPT-2' == get_post_type() && $post_5 == 0 && get_the_id() !== $post_2 ) {
            do_something_with_the_post();
            $post_5 = get_the_id();
            break;
        }
    }

    $my_query->rewind_posts();

    /*Sixth loop through posts*/
    while ( $my_query->have_posts() ) {
        $my_query->the_post();

        /**
         * Find the sixth post
        */
        if ( 'post' == get_post_type() && $post_6 == 0 && get_the_id() !== $post_3 ) {
            do_something_with_the_post();
            $post_6 = get_the_id();
            break;
        }
    }

    /**
     * And we're finished
     */
}
票数 1
EN

Stack Overflow用户

发布于 2021-12-17 15:50:32

我试图完成类似的任务,尽管Vayu的code is super elegant并没有达到我所需要的--具体来说,我需要能够为每个帖子类型包括一个不同的模板部分(它们的内容略有不同),并为WP帖子添加一个$arg (而不是CPT)来针对特定的类别。

我想要实现的是这一点(就像上面的$args逻辑格式):

代码语言:javascript
复制
    $my_post_types = array( 'auctions', 'liquidations', 'inventory',  'post' );

    // set args for ALL posts
    $args = array(
        'post_type'     => $my_post_types,
        'orderby'       => 'date',
        'order'         => 'DESC',
        'post_status'   => 'publish',
    );
    
    Now we loop through posts (while loop)

    IF 'post_type == auctions' 
        add number_posts to $args from ACF field for this post type
        get_template_part( 'template-parts/card', 'auction' );  

    IF 'post_type == liquidations' 
        add number_posts to $args from ACF field for this post type
        get_template_part( 'template-parts/card', 'liquidations' );  

    IF 'post_type == inventory' 
        add number_posts to $args from ACF field for this post type
        get_template_part( 'template-parts/card', 'inventory' );  

    IF 'post_type == post' 
        add 'cat => 304' to $args 
        add number_posts to $args from ACF field for this post type
        and get_template_part( 'template-parts/card', 'studies' );  

目标是在一个页面上显示管理中每种类型的qty集合中的所有CPT和Posts,并按日期排序。我需要更新/添加到每个post类型循环的$args中.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55559063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档