简体   繁体   English

WordPress排序自定义帖子类型

[英]Wordpress sort custom post type

I have a custom post type which displays all my entires upside down, meaning the first entrie is displayed last on my actual front page - is there a way to change the sort order? 我有一个自定义帖子类型,它的全部内容都上下颠倒显示,这意味着第一个条目在我的实际首页上最后显示-有没有办法改变排序顺序?

My code is as follows: 我的代码如下:

        <?php global $post; ?>

        <ul class="menu-items">
            <?php
            $menuloop = new WP_Query(array(
                'posts_per_page' => -1,
                'post_type'      => 'menu',
                'tax_query'      => array(
                    // Note: tax_query expects an array of arrays!
                    array(
                        'taxonomy' => 'menu_type', // my guess
                        'field'    => 'slug',
                        'terms'    => $menuname_category
                    )
                ),
            ));

            ?>
            <?php if ( have_posts() ) : while ( $menuloop->have_posts() ) : $menuloop->the_post(); ?>
            <li>
                    <div class="grid2column"><?php the_title(); ?></div>
                    <div class="grid2column lastcolumn"><?php if(get_post_meta($post->ID, 'menuoption_menu_pricing', true)): ?><?php echo get_post_meta($post->ID, 'menuoption_menu_pricing', true) ?><?php endif; ?></div>
                    <div class="clearfix"></div>
                    <div class="item-description-menu"><?php echo get_the_excerpt(); ?></div>
            </li>
            <?php endwhile; ?>
            <?php endif; ?>
        </ul>

I guess there is an error in the code you have given in the question. 我想您在问题中给出的代码中有错误。 posts_per_page => -1 . posts_per_page => -1

Why would it be -1 ? 为什么会为-1 It has to be a number > 0 for something to display. 要显示的内容,它必须是一个数字> 0

For sorting you need to add these two arguments to the array 为了进行排序,您需要将这两个参数添加到数组中

array ( 'orderby' => 'date', 'order' => 'DESC'

Basically, you can change the DESC to ASC to reverse the order. 基本上,您可以将DESC更改为ASC以颠倒顺序。

Final array 最终数组

$menuloop = new WP_Query(array(
                'posts_per_page' => 10,
                'post_type'      => 'menu',
                'orderby'        => 'date', // new arg
                'order'          => 'DESC', // new arg
                'tax_query'      => array(
                    // Note: tax_query expects an array of arrays!
                    array(
                        'taxonomy' => 'menu_type', // my guess
                        'field'    => 'slug',
                        'terms'    => $menuname_category
                    )
                ),
            ));

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

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