使用$ wpdb class

时间:2017-04-12 00:02:09

标签: mysql wordpress custom-wordpress-pages

如果帖子是粘性的,我试图使用自定义查询从数据库中获取特定的帖子信息。

以下是查询:

"SELECT ID, post_title, post_author, post_date, option_value
FROM `{$wpdb->prefix}posts`, `{$wpdb->prefix}options`
WHERE post_status = 'publish' AND post_type = 'post'
AND option_name = 'sticky_posts' 
ORDER BY post_date DESC LIMIT 2"

但是这个查询不起作用。它返回两个帖子,但不是粘性帖子。

1 个答案:

答案 0 :(得分:1)

好的想出来了。

我需要使用此代码并使用AND ID IN ($stringSticky)检查ID:

$sticky = get_option( 'sticky_posts' );         
$stringSticky = implode(",", $sticky);

$mostLatestPostsSticky = $wpdb->get_results(
    "SELECT ID, post_title, post_author, post_date
    FROM `{$wpdb->prefix}posts`
    WHERE post_status = 'publish' AND post_type = 'post'
    AND ID IN ($stringSticky)  
    ORDER BY post_date DESC LIMIT $postlimit"
);