Wordpress自定义POST查询

时间:2016-06-04 14:10:48

标签: php wordpress

我们说我在WordPress中有两类帖子,"重要"和"正常"。

我如何进行帖子查询,以便提取"重要帖子,按日期排序,最近一次排序"然后"普通帖子,按日期排序,最近一次",总共显示5?

(这可能是5个重要帖子,或1个重要帖子,然后4个普通,或​​5个普通等,具体取决于每个类别中的帖子数量。)

2 个答案:

答案 0 :(得分:2)

你可以使用WP_Query类来提取"重要"下的帖子。像这样的类别:

    <?php
       // GET 5 IMPORTANT POSTS... MOST RECENT 1ST...
        $args               = array(
            "post_type"         => 'post',
            "category_name"     => 'Important',     //<== CHANGE THIS TO "Normal" IF YOU WANT "Normal". IF YOU WANT BOTH CHANGE TO:'Important, Normal'
            'post_status'       => 'publish',
            "posts_per_page"    => 5,               // ONLY 5 IMPORTANT POSTS...
            'orderby'           => array(
                'post_date'       => 'DESC',        //<== MOST RECENT FIRST 
                "title"           => "ASC",
                'ID'              => 'ASC'
            ),
        );

        $importantPosts     = new WP_Query($args);
        // YOU CAN THEN LOOP THROUGH THE $importantPosts & DO WITH IT WHAT YOU WILL...


        // JUST ONE RECENT IMPORTANT POST...
        $args               = array(
            "post_type"         => 'post',
            "category_name"     => 'Important',     
            'post_status'       => 'publish',
            "posts_per_page"    => 1,
            'orderby'           => array(
                'post_date'       => 'DESC',
                "title"           => "ASC",
                'ID'              => 'ASC'
            ),
        );
        $importantPost      = new WP_Query($args);



        // 4 RECENT NORMAL POSTS...
        $args               = array(
            "post_type"         => 'post',
            "category_name"     => 'Normal',
            'post_status'       => 'publish',
            "posts_per_page"    => 4,
            'orderby'           => array(
                'post_date'       => 'DESC',
                "title"           => "ASC",
                'ID'              => 'ASC'
            ),
        );
        $normalPosts        = new WP_Query($args);



        // COMBINING 1 RECENT IMPORTANT POST WITH 4 RECENT NORMAL POSTS WITHIN A NESTED LOOP:
        if($importantPost->have_posts() && $normalPosts->have_posts()):
            while ( $importantPost->have_posts() ) :
                $importantPost->the_post();
                // SINCE THE IMPORTANT POST IS JUST ONE, LET IT BE ON THE OUTER LOOP AS IT WILL JUST RUN ONLY ONCE.
                // DO ALL YOU WISH TO WITH REGARDS TO THE IMPORTANT POST HERE
                the_title();
                echo "</br>";

                while ( $normalPosts->have_posts() ) :
                    // SINCE THE NORMAL POSTS ARE 4, LET IT BE ON THE INNER LOOP.
                    // DO ALL YOU WISH TO WITH REGARDS TO THE NORMAL POSTS HERE
                    $normalPosts->the_post();
                    the_title();
                    echo "</br>";
                endwhile;

            endwhile;
        endif;
        wp_reset_postdata();

答案 1 :(得分:0)

另一方面,您可能还想使用条件来另一条路线根据它们是重要还是正常来建立您的帖子,如下所示:

    <?php   
        // FETCH ALL EXISTING IMPORTANT POSTS ORDERED FROM NEWEST TO OLDEST
        $argsImp            = array(
            "post_type"         => 'post',
            "category_name"     => 'Important',
            'post_status'       => 'publish',
            "posts_per_page"    => -1,
            'orderby'           => array(
                'post_date'       => 'DESC',
                "title"           => "ASC",
                'ID'              => 'ASC'
            ),
        );
        $importantPosts     = new WP_Query($argsImp);


        // FETCH ALL EXISTING NORMAL POSTS ORDERED FROM NEWEST TO OLDEST
        $argsNorm           = array(
            "post_type"         => 'post',
            "category_name"     => 'Normal',
            'post_status'       => 'publish',
            "posts_per_page"    => -1,
            'orderby'           => array(
                'post_date'       => 'DESC',
                "title"           => "ASC",
                'ID'              => 'ASC'
            ),
        );
        $normalPosts        = new WP_Query($argsNorm);

        // CHECK IF WE HAVE UP TO 5 RECENT IMPORTANT POSTS.
        // IF WE DO, WE IGNORE THE NORMAL POST & ENTER OUR LOOP...
        if(count($importantPosts->posts)>=5){
            // WE KNOW WE HAVE AT LEAST 5 RECENT IMPORTANT POSTS...
            $impCounter = 0;
            while ( $importantPost->have_posts()  && $impCounter < 5) :
                $importantPost->the_post();
                // SINCE THE IMPORTANT POSTS ARE 5, WE CAN IGNORE THE NORMAL POST, THOUGH.
                // DO ALL YOU WISH TO WITH REGARDS TO THE IMPORTANT POST HERE
                the_title(); echo "</br>";
                $impCounter++;
            endwhile;
            wp_reset_postdata();

        // IF OTHERWISE WE HAVE AT LEAST 1 IMPORTANT POST & 4 NORMAL POSTS,
        // WE ENTER A LOOP; GETTING 4 NORMAL POSTS & 1 IMPORTANT POST FIRST.
        }else if(count($normalPosts->posts)>=4 && count($importantPosts->posts)>=1){
            // WE KNOW WE HAVE AT LEAST A 5 RECENT NORMAL POST AND AT LEAST 1 RECENT IMPORTANT POST...
            $impCounter = 0;
            $nmlCounter = 0;
            // COMBINING 1 RECENT IMPORTANT POSTS WITH 4 NORMAL POSTS WITHIN A NESTED LOOP:
            while ( $importantPosts->have_posts()  && $impCounter < 1) :
                $importantPost->the_post();
                // SINCE THE IMPORTANT POSTS COULD BE MANY... WE USE THE $impCounter VARIABLE TO LIMIT IT TO 1 ITERATION REGARDLESS OF ITS LENGTH.
                // DO ALL YOU WISH TO WITH REGARDS TO THE IMPORTANT POST HERE
                the_title(); echo "</br>";

                while ( $normalPosts->have_posts()  && $nmlCounter < 5) :
                    // SINCE THE RECENT NORMAL POSTS ARE AT LEAST 4, LET IT BE ON THE INNER LOOP...WE USE THE $nmlCounter VARIABLE TO LIMIT IT TO 5 ITERATIONS
                    // DO ALL YOU WISH TO WITH REGARDS TO THE NORMAL POSTS HERE
                    $normalPosts->the_post();
                    the_title(); echo "</br>";

                    $nmlCounter++;
                endwhile;

                $impCounter++;
            endwhile;

        // YET, IF WE HAVE NO IMPORTANT POST AT ALL BUT UP TO 5 NORMAL POSTS,
        // AGAIN WE ENTER A LOOP: THIS TIME DEALING ONLY WITH NORMAL POSTS.
        }else if(count($normalPosts->posts)>=5 && count($importantPosts->posts)<1){
            // WE KNOW WE HAVE AT LEAST A 5 RECENT NORMAL POSTS BUT NO IMPORTANT POST...
            $nmlCounter = 0;
            while ( $normalPosts->have_posts()  && $nmlCounter < 5) :
                $normalPosts->the_post();
                // SINCE THE IMPORTANT POSTS ARE 5, WE CAN IGNORE THE NORMAL POST, THOUGH.
                // DO ALL YOU WISH TO WITH REGARDS TO THE IMPORTANT POST HERE
                the_title(); echo "</br>";
                $nmlCounter++;
            endwhile;
            wp_reset_postdata();
        }
        wp_reset_postdata();