简体   繁体   English

WordPress自定义帖子类型查询

[英]Wordpress Custom Post Type Query

I have 4 custom post types, say for example pt1, pt2, pt3 and pt4. 我有4种自定义帖子类型,例如pt1,pt2,pt3和pt4。

They all use the same taxonomies to categories them. 它们都使用相同的分类法对它们进行分类。 On the taxonomy archive page, I would like the posts to be grouped by post-type, so any posts in pt1 appear first, then pt2, pt3 etc - at the moment they use the default ordering (which I assume is post date) so are all mixed up. 在分类存档页面上,我希望按帖子类型对帖子进行分组,因此pt1中的所有帖子首先出现,然后是pt2,pt3等-在它们使用默认顺序(我认为是发布日期)的同时,都混了。

I tried to run separate queries for each post type, but this messed up the pagination. 我尝试为每种帖子类型运行单独的查询,但是这弄乱了分页。 Thanks in advance! 提前致谢!

You can use following in archive page; 您可以在存档页面中使用以下内容;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$limit = 10;
$offset = ( $paged -1 ) * 10; 
$query = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    ORDER BY $wpdb->posts.post_type DESC
    LIMIT $offset, $limit
 ";

 $custom_posts = $wpdb->get_results($query, OBJECT);



if ( $custom_posts ) : 
    global $post;
    foreach ($custom_posts as $post) {
        setup_postdata($post);
        the_post(); 
        get_template_part( '/partials/content-archive', get_post_format() );
    }
    the_bootstrap_content_nav(); 
endif;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM