wordpress orderby = rand除非ACF字段= true

时间:2016-01-04 12:02:50

标签: php wordpress advanced-custom-fields

这可能听起来像是一个奇怪的问题,但是......

我有一个客户要求发布结果随机排序(很容易)...但他们也希望能够选择帖子"特色"并显示在列表的顶部,其他帖子随机排列在下面。

我尝试过一个包含以下内容的循环:

<?php global $query_string; query_posts( $query_string . '&orderby=rand' );
if (have_posts()): while (have_posts()) : the_post(); ?>
    <?php if (get_field('featured_listing') == 1):?>
         list of featured posts
    <?php endif ;?>
    <?php if (get_field('featured_listing') != 1):?>
         all the other posts posts
    <?php endif ;?>
<?php endwhile; ?>
<?php endif; ?>

但特色帖子和非特色帖子都是随机排列的。

然后我尝试创建两个队列:

<?php  global $query_string; query_posts( $query_string . '&orderby=rand' );
if (have_posts()): while (have_posts()) : the_post(); ?>
    <?php if (get_field('featured_listing') == 1):?>
           list of featured posts
    <?php endif ;?>
<?php endwhile; ?>
<?php endif; ?>
<?php 
global $query_string;
query_posts( $query_string . '&orderby=rand' );
if (have_posts()): while (have_posts()) : the_post(); ?>
    <?php if (get_field('featured_listing') != 1):?>
          all the other posts posts
    <?php endif ;?>
<?php endwhile; ?>
<?php endif; ?>

但是当页面加载时,它们仍然作为一个随机列表加载。如果您刷新页面几次,精选帖子会显示在列表的顶部,但这并不是很好。

这是一个ACF问题还是我忽略了一些非常简单的事情?有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:1)

永远不要使用query_posts()。查询数据库的帖子真的很糟糕。如果您需要更改主查询(真实页面除外),则应使用pre_get_posts,否则,如果您确实需要自定义查询,请使用WP_Query

要解决您的问题,只需重新运行相同的循环,一次输出精选帖子,第二次显示其余的

$args = [
    'orderby' => 'rand',
    // Any other arguments
];
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
    // Run the loop for the first time to show featured posts
    while ( $q->have_posts() ) {
    $q->the_post();

        // Get the field value
        $field = get_field( 'featured_listing', get_the_ID() );
        if ( true === $field ) { // Featured posts
            // Output featured posts
            the_title();
        }

    } // endwhile, end of loop one

    // Rewind the loop to run it again
    $q->rewind_posts();

    // Run the loop for the second time to show other posts
    while ( $q->have_posts() ) {
    $q->the_post();

        // Get the field value
        $field = get_field( 'featured_listing', get_the_ID() );
        if ( true !== $field ) { // Other posts
            // Output other posts
            the_title();
        }

    } // endwhile, end of loop
    wp_reset_postdata(); // EXTREMELE IMPORTANT FOR CUSTOM LOOPS
} //endif

修改

正如我所说,如果您需要更改主查询,请使用pre_get_posts。由于这是category.php,您绝对不希望用自定义查询替换主查询。我们需要在主查询运行之前更改主查询变量。

add_action( 'pre_get_posts', function ( $q )
{
    if (    !is_admin() // Only target the front end
         && $q->is_main_query() // Only target the main query
         && $q->is_category() // Only target category pages
    ) {
        // Set our custom ordering to random
        $q->set( 'orderby', 'rand' );
    }
});

这会将类别页面上的排序设置为随机。我们现在需要做的就是删除自定义查询并将其替换为默认循环

if ( have_posts() ) {
    // Run the loop for the first time to show featured posts
    while ( have_posts() ) {
    the_post();

        // Get the field value
        $field = get_field( 'featured_listing', get_the_ID() );
        if ( true === $field ) { // Featured posts
            // Output featured posts
            the_title();
        }

    } // endwhile, end of loop one

    // Rewind the loop to run it again
    rewind_posts();

    // Run the loop for the second time to show other posts
    while ( have_posts() ) {
    the_post();

        // Get the field value
        $field = get_field( 'featured_listing', get_the_ID() );
        if ( true !== $field ) { // Other posts
            // Output other posts
            the_title();
        }

    } // endwhile, end of loop
} //endif

答案 1 :(得分:0)

您可以将值保存在变量中,这样您就可以构建数据/代码并按照您需要的顺序输出它,例如......

更新以显示更好(不完美)的代码

<?php
// Declare the variables
$featured_posts = null;
$other_posts = null;

// Define our arguments
$args = array(
    'orderby' => 'rand'
);

// Now get the posts
$the_query = new WP_Query( $args );

// Check we have some posts
if ( $the_query->have_posts() ) {
    // Then loop through them
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        // Build up your lists - add in whatever you need - title shown as an example
        if(get_post_meta( get_the_ID(), 'featured_listing', true ) == 1) {
            $featured_posts .= '<h2>'.get_the_title().'<h2>';
        } else {
            $other_posts .= '<h2>'.get_the_title().'<h2>';
        }
    }

} else {
    // no posts found
}

wp_reset_postdata(); // As Pieter says it is important to reset the post data

// Now output the lists in featured at the top
echo $featured_posts;
echo $other_posts;
?>