最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Display category posts before others category custom query

programmeradmin2浏览0评论
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 5 years ago.

Improve this question

I have a page display some posts categories. So I want display before all post in my category 1 then, the others categories...

So I want have: - display all posts in category 1 with descendent data after all posts I want display all other categories

thanks


I find the solution: So I have the first query args:

$prima = array(
                'post_type' => 'post',
                'order'          => 'DESC',
                'orderby'        => 'date',
                //  'cat' => 41,
                'category__in' => array('41'),
                'post_status' => 'publish',
                'suppress_filters' => 0,
                'nopaging' => true,
            );

The second query args

$seconda = array(
                'post_type' => 'post',
                'order'          => 'DESC',
                'orderby'        => 'date',
                //  'cat' => 41,
                'category__in' => array('42,43'),

                'post_status' => 'publish',
                'suppress_filters' => 0,
                'nopaging' => true,
            );

So I find the post ids from the 2 queries and I add in the same array

// unique array
$array_primo_secondo = array();

// first loop
if ($query_prima->have_posts()) : while ($query_prima->have_posts()) : $query_prima->the_post();

                    $post_id_prima = get_the_ID();  
                    $array_primo_secondo[] =  $post_id_prima;
                endwhile;
            endif;

// second loop
if ($query_seconda->have_posts()) : while ($query_seconda->have_posts()) : $query_seconda->the_post();

                    $post_id_seconda = get_the_ID();
                    $array_primo_secondo[] =  $post_id_seconda;
                endwhile;
            endif;

Now I have the array with all the ID So I can do the last query

$args = array(
                // all the post in my array
                'post__in' => $array_primo_secondo,
                // I have the original pagination
                'paged' => $paged,
                'suppress_filters' => 0,
                // I have the order in the array
                'orderby' => 'post__in',
            );

            $wp_query = new WP_Query($args);

Thanks

发布评论

评论列表(0)

  1. 暂无评论