简体   繁体   English

如何在此wordpress查询中添加order&orderby参数?

[英]How do I add order & orderby parameters to this wordpress query?

I managed to paint myself into a corner when using this snippet, but I can't manage to work out how to sort the query. 使用此代码段时,我设法将自己描绘成一个角落,但是我无法设法确定如何对查询进行排序。 Usually I can do it but with this snippet that excludes one tag on the tag page I can't really work it out. 通常,我可以这样做,但是使用不包含标签页面上一个标签的此代码片段,我无法真正解决该问题。 Anyone? 任何人?

$exclude_tags = array(17);

global $wp_query;
$wp_query->set('tag__not_in', $exclude_tags);

$wp_query->get_posts();

if (have_posts()) : while (have_posts()) : the_post();

This example will be help full for you :- 这个例子将为您提供充分的帮助:-

$args = array(
        'post_type' => 'post',
        'meta_key' => 'pb_issue_featured',
        'orderby'   => 'meta_value',
        'order' => 'DESC',
        'posts_per_page' => $posts,
        'paged' => $paged,
        'paged' => 1,
        'meta_query' => array(
            array(
                'key' => 'headline',
                'value' => 1,
                'compare' => '!=' 
                )
            )
        );

add_filter( 'posts_orderby', 'filter_query' );
$q = new WP_Query($args);
remove_filter( 'posts_orderby', 'filter_query' );

function filter_query( $query ) {
    $query .= ', wp_posts.menu_order ASC';
    return $query;
}

Referenced From 引用自

Please look at this example. 请看这个例子。 You can do like this. 你可以这样

The code will display the title of last ten posts sorted alphabetically in ascending order. 该代码将显示按字母顺序升序排列的最近十个帖子的标题。

<?php
    $args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
    $postslist = get_posts( $args );
    foreach ( $postslist as $post ) :
      setup_postdata( $post ); ?> 
        <div>
            <?php the_title(); ?>
        </div>
    <?php
    endforeach; 
    wp_reset_postdata();
?>

Please refer the link http://codex.wordpress.org/Template_Tags/get_posts for more details. 请参阅链接http://codex.wordpress.org/Template_Tags/get_posts了解更多详细信息。

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

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