【问题标题】:Display 'X' amount of posts within multiple queries在多个查询中显示“X”个帖子
【发布时间】:2017-07-07 01:13:21
【问题描述】:

我目前在我的网站上显示事件,基于它们被“标记”的月份。对于每个月,我都进行了查询以显示该月内的帖子,按帖子过期日期排序。

我正在寻找一种更简单的方法(或者将其组合为 1 个查询,而不是多个)来显示(例如)所有这些查询中的前 50 个帖子。..

代码:

$argsJanuari = [
    'numberposts'   => -1,
    'cat'           => '-1',
    'order'         => 'ASC',
    'meta_key'      => '_expiration-date',
    'orderby'       => 'meta_value',    
    'meta_query'    => array(
        array(
            'key'   => 'maand',
            'value' => 'Januari 2017',
        )
    )
];

$loopJanuari = new WP_Query($argsJanuari);
    if($loopJanuari->have_posts()) { ?> <h2>Januari 2017</h2> <?php
        while($loopJanuari->have_posts()) {
            $loopJanuari->the_post();
            get_template_part( 'concert-single' );
        }
    wp_reset_postdata();
}

$argsFebruari = [
    'numberposts'   => -1,
    'cat'           => '-1',
    'order'         => 'ASC',
    'meta_key'      => '_expiration-date',
    'orderby'       => 'meta_value',    
    'meta_query'    => array(
        array(
            'key'   => 'maand',
            'value' => 'Februari 2017',
        )
    )
];

$loopFebruari = new WP_Query($argsFebruari);
    if($loopFebruari->have_posts()) { ?> <h2>Februari 2017</h2> <?php
        while($loopFebruari->have_posts()) {
            $loopFebruari->the_post();
            get_template_part( 'concert-single' );
        }
    wp_reset_postdata();
}

$argsMaart = [
    'numberposts'   => -1,
    'cat'           => '-1',
    'order'         => 'ASC',
    'meta_key'      => '_expiration-date',
    'orderby'       => 'meta_value',    
    'meta_query'    => array(
        array(
            'key'   => 'maand',
            'value' => 'Maart 2017',
        )
    )
];

$loopMaart = new WP_Query($argsMaart);
    if($loopMaart->have_posts()) { ?> <h2>Maart 2017</h2> <?php
        while($loopMaart->have_posts()) {
            $loopMaart->the_post();
            get_template_part( 'concert-single' );
        }
    wp_reset_postdata();
}    

?>

【问题讨论】:

  • 过滤器 post_limits ?

标签: php wordpress loops posts


【解决方案1】:

您可以使用元查询关系将所有查询合并为一个。更多信息在这里:https://codex.wordpress.org/Class_Reference/WP_Meta_Query。此外,要设置帖子的数量,帖子参数称为posts_per_page。我不确定你使用的是'numberposts',所以我在下面的代码中替换了它。

无论如何,在代码下方找到合并的查询和显示的 50 个帖子。它没有经过测试和编写,并认为您在上面发布的代码有效。让我知道这是否有帮助:)

$argsConcert = [
    'posts_per_page'   => 50,
    'cat'           => '-1',
    'order'         => 'ASC',
    'meta_key'      => '_expiration-date',
    'orderby'       => 'meta_value',    
    'meta_query'    => array(
        'relation'  => 'OR',
        array(
            'key'   => 'maand',
            'value' => 'Januari 2017',
        )
        array(
            'key'   => 'maand',
            'value' => 'Februari 2017',
        )
        array(
            'key'   => 'maand',
            'value' => 'Maart 2017',
        )
    )
];

$loopConcert = new WP_Query($argsConcert);
    if($loopConcert->have_posts()) { ?> <h2>Concert 2017</h2> <?php
        while($loopConcert->have_posts()) {
            $loopConcert->the_post();
            get_template_part( 'concert-single' );
        }
    wp_reset_postdata();
}    

【讨论】:

  • 你好,这几乎是我想要的。显示的帖子数量是正确的。但是我每个月都分配了所有帖子。 (如果在 2 月有帖子,请显示

    2017 年 2 月

    。使用提供的代码,帖子现在是 1 个没有月份分隔符的长列表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 2018-01-19
相关资源
最近更新 更多