duanmu5039 2017-05-04 17:04
浏览 40

Wordpress搜索结果将自定义帖子类型中的主要帖子分开

I have a custom post type named 'Artists'. My current search results are pulling this post type along with my standard posts in one grouping. I am having trouble separating the two posts types in my results.

Instead of...

  • Blog Post
  • Blog Post
  • Artist Post
  • Blog Post
  • Artist Post

I'd like....

BLOG POSTS:

  • Blog Post
  • Blog Post
  • Blog Post

ARTIST POSTS:

  • Artist Post
  • Artist Post

Is that possible? I am not sure if I can solve this with one loop/query or it would be faster to use two queries?

Current code is here...

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">
        <?php the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>

        <?php if ( 'post' == get_post_type() ) : ?>
        <div class="entry-meta">
            <?php the_date(); ?>
        </div><!-- .entry-meta -->
        <?php endif; ?>
    </header><!-- .entry-header -->

    <div class="entry-summary">
        <?php the_excerpt(); ?>
    </div><!-- .entry-summary -->
</article><!-- #post-## -->

Any help appreciated. I realize there are some similar questions on this already, but most have been unhelpful for this particular situation.

  • 写回答

1条回答 默认 最新

  • dp6319 2017-05-04 17:16
    关注

    Just change the order of the wp_query object

    $args = array(
        'order' => 'type'
    );
    $query = new WP_Query( $args );
    

    OP is to lazy to read the Docs so here i provide an example:

    $args = array('order' => 'type');
    $my_query = new WP_Query( $args );
    while ( $my_query->have_posts() ) {
     //
    }
    

    EDIT: Here another possible solution if you want to avoid a new Instance of wp-query and use the global Object:

    add_filter('posts_orderby','my_sort_custom',10,2);
    function my_sort_custom( $orderby, $query ){
        global $wpdb;
    
        if(!is_admin() && is_search()) 
            $orderby =  $wpdb->prefix."posts.post_type ASC"
    
        return  $orderby;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP