Wordpress排序帖子/自定义帖子类型

时间:2015-08-18 08:51:02

标签: php wordpress

我有这个自定义循环,出于某种原因"排序"没有工作。

<?php   
                    if(isset($_REQUEST['sort'])){
                        if($_REQUEST['sort'] == 'newest' )
                            $order = "&orderby=title&order=DESC"; 
                        else if($_REQUEST['sort'] == 'oldest' )
                            $order = "&orderby=title&order=ASC";    
                        else if($_REQUEST['views'] == 'oldest' )
                            $order = "&meta_key=views&orderby=meta_value_num&order=DESC";       
                    }
                    else 
                        $order = "&orderby=ID&order=DESC"; 
                    ?>
                    <form method="post" id="order">
                        <select name="sort" onchange='this.form.submit()'>
                            <option value="newest">Sort by Newest</option>
                            <option value="oldest">Sort by Oldest</option>
                            <option value="views">Sort by Most Viewed</option>
                        </select>
                    </form>

                <ul class="acapellas row">
                <?php   
                    $loop = new WP_Query( array( 
                        'post_type' => 'acapella',
                        'posts_per_page' => 10,
                        'paged' => $paged,
                        'orderby' => 'date',
                        'order' => $_POST['sort'] 
                    ) ); 
                ?>
                <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
                <?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

                 <?php $posts = query_posts($query_string . $order); ?>

                    <li class="post-<?php the_ID(); ?> col-md-6">
                        <div class="wrap"> 

                            <h2><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h2>

                            <?php if(pmpro_hasMembershipLevel($level_id)) { ?>

                            <?php the_content(); ?>

                            <?php } else { ?>

                            <div class="pro-player">
                                <div class="upgrade">
                                    <a href="<?php bloginfo('url'); ?>/pro" >Upgrade to unlock</a>
                                </div>
                            </div>

                            <?php } ?>

                            <a class="download left" href="<?php the_permalink(); ?>">Download</a>

                            <span class="list-date right">First added: <?php the_time('F jS, Y') ?></span><br>

                           <?php
                            global $post;
                            $post_type            = get_post_type(get_the_ID());
                            $post_type_taxonomies = get_object_taxonomies($post_type);

                            if (!empty($post_type_taxonomies)) {

                                echo '<ul class="details">';

                                foreach ($post_type_taxonomies as $taxonomy) {

                                    $terms = get_the_term_list(get_the_ID(), $taxonomy, '', '</li><li>', '');


                                    if ($terms) {
                                        echo '<li>' . $terms . '</li>';
                                    }
                                }

                                echo '</ul>';
                            }
                            ?>
                        </div>
                    </li>
                <?php endwhile; endif; ?>
                </ul>

1 个答案:

答案 0 :(得分:0)

您可以使用功能get_posts()

$posts = get_posts([
    'orderby' => 'date',
    'order' => $_POST['sort'] 
]);

并发送帖子变量&#39; ASC&#39;或者&#39; DESC&#39; - 这将是最新的或最早的