【问题标题】:wordpress orderby=rand unless ACF filed = truewordpress orderby=rand 除非 ACF 归档 = true
【发布时间】:2016-01-04 12:02:50
【问题描述】:

这听起来有点奇怪,但是..

我有一个客户要求对帖子结果进行随机排序(简单易行)。但他们也希望能够选择要“精选”的帖子并出现在列表顶部,其他帖子排序随机在下面。

我尝试过使用以下循环:

<?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 问题还是我忽略了一些非常简单的问题?谁能指出我正确的方向吗?

【问题讨论】:

  • 永远不要使用query_posts()。它破坏了主要查询对象,进而破坏了依赖于主要查询对象的数千个其他功能。此外,您的一个查询正在破坏另一个查询。除了破坏东西之外,query_posts() 真的很慢并且会重新运行查询,这会减慢您的页面速度,这意味着糟糕的 SEO 排名。我不知道您在哪个页面上使用它,但您始终可以使用 pre_get_posts 修改主查询的查询变量,或者如果您真的需要自定义查询,请使用 WP_Query

标签: php wordpress advanced-custom-fields


【解决方案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

【讨论】:

  • 这与上面的问题相同,因为它不喜欢custome字段。我尝试将字段的 meta_key 和 meta_value 添加到返回正确结果的数组中,但它现在返回值为 1 的所有内容,无论类别如何。有没有一种简单的方法可以将类别 ID 放入数组中?
  • 这正是在哪个页面/模板上
  • 它在 category.php 上,所以 url 类似于 ../category/suppliers/
  • AHA,那么您根本不应该使用自定义查询。将更新我的答案
  • 它只管用!! :) 将 1 替换为 true 即可!
【解决方案2】:

您可以将值保存在变量中,以便构建数据/代码并按您需要的顺序分组输出,例如...

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

<?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;
?>

【讨论】:

  • 可能是个好主意。更好的主意是运行循环输出精选帖子rewind_posts() 并再次重新运行循环以显示其他帖子。只是一个 HHHHUUUUUUGGGGGEEEEE 提示,永远不要使用 query_posts,它会破坏主要查询对象,进而破坏依赖于主要查询对象完整性的数千个其他功能和插件
  • 同意 query_posts - 我只是使用他们必须避免完全为他们编写代码的东西。如果您想要混合数量的“精选帖子”,我认为一个查询很好,但是如果您对固定数量(4 个精选帖子和 6 个其他帖子)感到满意,那么您可以使用两个查询。无论哪种方式,如果您打算进行自定义查询,请使用 wp_query() codex.wordpress.org/Class_Reference/WP_Query
  • 谢谢。这似乎是实现我需要的更好的方法。虽然,我现在遇到的问题是 WP_Query 忽略了自定义字段查询并且不输出 features_posts。
  • 你的意思是 get_field('featured_listing') 不再起作用了吗?
  • 没错。我在自定义字段上尝试了一些变体。这似乎是 ACF 和 WP_Query 的常见问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多