WordPress query_posts orderby rand 在类别存档模板中不起作用



我无法让我的 Wordpress 主题随机化我在类别存档中显示的帖子 [我将其用作 CMS]。主页正常随机化,我[我认为]正确地更改了WP_query。下面是确切的参数数组:

array(4) { ["orderby"]=> string(4) "rand" ["order"]=> string(3) "ASC" ["posts_per_page"]=> string(2) "-1" ["category_name"]=> string(8) "branding" }

为了便于阅读,它是:

orderby => rand
order => ASC
posts_per_page => -1
category_name => branding (or whatever the query_string brings in)

我从该类别中获取所有帖子,但它们按发布日期顺序排列。

有什么线索吗? 还是在have_posts中洗牌我的WP_query结果的替代方法?

谢谢。

************EDIT************

抱歉,我应该更清楚上面的 args 数组。它是查询数组的var_dump,而不是我添加到查询中的参数。

    $args = array(
        'orderby'        => 'rand',
        'order'      => 'ASC',
        'posts_per_page' => '-1',
    );
    global $wp_query;           
    remove_all_filters('posts_orderby');
    $theq = array_merge($args, $wp_query->query);
    query_posts($theq);

我根据谢赫·希拉的建议添加了remove_all_filters,但它并没有产生任何影响。

那你最好创建一个新查询。这应该只用于分类模板,尽管如类别.php或分类-你的自定义分类.php。

global $wp_query;
$term = $wp_query->queried_object;
$args=array(
    'orderby' => 'rand',
    'posts_per_page' => -1,
    'post_type' => 'post',
    'tax_query' => array(
            array(
                'taxonomy'  => $term->taxonomy,
                'field'     => 'slug',
                'terms'     => $term->slug,
                )
            )
    );
$new_query = null;
$new_query = new WP_Query($args);
while ($new_query->have_posts()) : $new_query->the_post(); ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <div class="entry-meta"><?php // Meta ?></div><!-- .entry-meta -->
        <div class="entry-content"><?php the_content(); ?></div>
    </div>
<?php
endwhile;
wp_reset_postdata();

它可能是另一个产生问题的插件,但您可以执行以下操作

remove_all_filters('posts_orderby');
$args=array(
    orderby => 'rand'
    order => 'ASC'
    posts_per_page => -1
    category_name => 'branding'
);
query_posts($args);

请记住,您可以使用此解决方案破坏插件的功能,但它可能对解决问题很有用,但可能并不完美。

我认为您想将其与原始查询合并。然后无需指定类别,如果它也以这种方式使用自定义分类法,则可以工作。

$args = array(
    'posts_per_page' => -1,
    'orderby' => 'rand'
    );
query_posts( array_merge( $wp_query->query, $args) ); 

最新更新