简体   繁体   English

WordPress通过SQL查询获取帖子

[英]Wordpress get post via sql query

I grabbed the code that wordpress uses to display the default calendar and it gets the dates like this: 我抓取了wordpress用于显示默认日历的代码,它获取的日期如下:

if ( $day == current_time( 'j' ) &&
    $thismonth == current_time( 'm' ) &&
    $thisyear == current_time( 'Y' ) ) {
}

Using those values (day, month and year), how can I write a mysql query that pulls the post created on that specific date? 使用这些值(日,月和年),如何编写mysql查询来提取在该特定日期创建的帖子?

I tried to have this link as reference but my sql skills are close to none. 我试图将此链接作为参考,但是我的SQL技能几乎没有。

EDITED: 编辑:

My table name is "wp_posts" and the date column is named as post_date. 我的表名称是“ wp_posts”,日期列名为post_date。

Accosrding to the answer here I did the following: 根据这里的答案,我做了以下工作:

$date = new DateTime($thisyear."-".$thismonth."-".$day);

$date->setTime($hours, $minutes, $seconds);

$date_format = $date->format("Y-m-d H:i:s");

$request = $pdo->query("SELECT * FROM $wpdb->posts WHERE post_date = ".$date_format);

$result = $request->fetch();

but when I var_mp $result, nothing happens 但是当我var_mp $ result时,什么也没发生

var_dump($result);

If the field of your database is type of "DATE" you can (with PDO): 如果数据库的字段是“ DATE”类型,则可以(使用PDO):

$date = new DateTime($thisyear."-".$thismonth."-".$day); //__Create my object date

$date_format = $date->format("Y-m-d"); //__Date formatting

$request = $pdo->query("SELECT * FROM my_table WHERE date_article = ".$date_format); //__Create my SQL query

$result = $request->fetch() //__Or 'fetchAll' if many result could be possible

EDIT 编辑


If the field of your database is type of "DATETIME" you can (with PDO): 如果数据库的字段是“ DATETIME”类型,则可以(使用PDO):

$date = new DateTime($thisyear."-".$thismonth."-".$day); //__Create my object date

$date->setTime($hours, $minutes, $seconds);

$date_format = $date->format("Y-m-d H:i:s"); //__Date formatting

$request = $pdo->query("SELECT * FROM my_table WHERE date_article = ".$date_format); //__Create my SQL query

$result = $request->fetch() //__Or 'fetchAll' if many result could be possible

$result will contain your array with your article(s). $result将包含您的文章数组。

Sorry I can't comment on the above answer yet. 抱歉,我无法对以上答案发表评论。 So I'm going to post my comment as an answer. 因此,我将发表我的评论作为答案。

I believe noting is being returned because your query doesn't match any value. 我认为会返回注释,因为您的查询不匹配任何值。

First 第一

Dump $date_format and see the output. 转储$date_format并查看输出。

Secondly 其次

Compare the output you get with the values in post_date 将您获得的输出与post_date的值进行post_date

Lastly 最后

Edit the build up of $date_format to match the values in post_date accordingly. 编辑$date_format的构建以相应地匹配post_date中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM