po-6ren">
gpt4 book ai didi

mysql查询类别和日期

转载 作者:行者123 更新时间:2023-11-30 22:43:43 24 4
gpt4 key购买 nike

如何查询 wordpress 数据库,以便只显示从特定日期开始的特定类别的帖子数量?

我试过类似的方法,但它不起作用:

<?php
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE term_id = '4' AND post_date >= '2014-01-01 00:00:00' " );
echo "<p>User count is {$user_count}</p>";
?>

我做错了什么?

最佳答案

使用 wordpress 原生 WP-Query:

$args = array(
'post_type' => 'post',
'date_query' => array(
'year ' => 2015,
),
'cat' => 5,
'posts_per_page'=> -1
);
$query = new WP_Query( $args );
$numberOfPosts = $query->post_count;

$numberOfPosts 应该包含您要查找的号码。只需将此粘贴到您使用与我们共享的原始代码的位置即可。

在这里阅读更多:https://codex.wordpress.org/Class_Reference/WP_Query

摘自上述网址:

posts_per_page (int) - number of post to show per page (available with Version 2.1, replaced showposts parameter). Use 'posts_per_page'=>-1 to show all posts (the 'offset' parameter is ignored with a -1 value). Set the 'paged' parameter if pagination is off after using this parameter. Note: if the query is in a feed, wordpress overwrites this parameter with the stored 'posts_per_rss' option. To reimpose the limit, try using the 'post_limits' filter, or filter 'pre_option_posts_per_rss' and return -1

您还可以在那里找到许多其他选项来调整您的查询,以获得所需的结果。这应该会给出您现在想要的结果。

关于mysql查询类别和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30276423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com