【问题标题】:Wordpress - Count all posts that have a future date to the current oneWordpress - 将所有具有未来日期的帖子计数到当前日期
【发布时间】:2019-10-04 18:46:33
【问题描述】:

我正在寻找那里将未来日期的所有帖子计数到当前的帖子

查询:

<?php
    $mostra_data_corrente = date('d-m-Y');
    $query = new WP_Query( array(
        'post_type' => get_option('customer_postquery'),
        'post_status' => 'publish',
        'meta_query' => array(
        array(
            'key' => 'metakey_AMC_data',
        )
    ),
    'date_query' => array(
        'after' => $mostra_data_corrente,
    ),
    'tax_query' => array(
        array(
            'taxonomy' => 'categoria',
            'field' => 'slug',
            'terms' => $queried_object,
        ) 
    ) ) ) ;
    $conta_risultati = $query->found_posts;
    echo $conta_risultati;
?>

地点:

get_option('customer_postquery'):动态检索所有已创建的自定义帖子类型

metakey_AMC_data: 是元键,其中事件(发布)的日期包含在元值中

$queried_object:根据我们所在的页面动态检索帖子的分类,从而根据它们的分类过滤帖子

所以我的意图是计算所有有未来日期的帖子到当前的日期

“与当前日期相比,未来日期存在多少帖子并不能完全计算”

编辑代码:

<?php
$mostra_data_corrente = date('d-m-Y');
$query = new WP_Query( array(
    'post_type' => get_option('customer_postquery'),
    'post_status' => 'publish',
    'meta_query' => array(
        array(
            'key'       => 'metakey_AMC_data',
            'compare'   => '>',
        )
    ),
    'date_query'     => array(
        'after' => $mostra_data_corrente,
    ),
    'tax_query' => array(
        array(
            'taxonomy' => 'categoria',
            'field' => 'slug',
            'terms' => $queried_object,
        )
    )
) ) ;
$conta_risultati = $query->found_posts;
echo $conta_risultati;

result:3 ,但这不是事实,因为我在这个分类的当前日期之后有 7 个结果

【问题讨论】:

  • 还有,实际的具体问题是什么?你需要解释你所尝试的有什么问题。
  • @04FS 我认为从我发布问题的那一刻起就很明显可以理解,与当前相比,未来日期存在多少帖子并不能完全计算
  • 我认为你可以在 date_query 中尝试使用 before 和 after 方法
  • @Jinesh 我在今天之前和今天之后尝试了这个,但没有工作:(
  • 你能回显这个东西 get_option('customer_postquery'); 吗?

标签: php wordpress


【解决方案1】:

可能你需要像这样查询它们:

$currentPostDate = 'get your post date here';
$args = array(
    'post_type' => 'post',
    'meta_key' => 'the_date',
    'meta_query' => array(
        array(
            'key' => 'the_date',
            'value' => $today,
            'compare' => '>='
        )
    ),
);

$your_custom_query = new WP_Query($args);
$postcount = count($your_custom_query);

考虑在你的functions.php文件中让它成为函数。

【讨论】:

    猜你喜欢
    • 2018-02-04
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2014-03-20
    • 2018-10-25
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多