在我的Wordpress设置中,每个帖子都有一个附加了meta_key = visitor_tracking_visitor_id的帖子元,这是帖子后的作者ID .
我正在尝试从Wordpress wp_posts表中提取帖子列表,以及针对该给定作者ID的status = publish的帖子数 .
Tables
wp_posts
id | post_title | post_content | post_status(“发布”或“垃圾”)
wp_postmeta
post_id(加入wp_posts的id)| meta_key | meta_value
当meta_key = visitor_tracking_visitor_id时,meta_value保存作者ID
Example expected result:
| id | meta_value(作者ID)| count_of_posts_by_that_meta_value |
| 1 | 1234 | 10 |
| 2 | 1234 | 10 |
| 3 | 5678 | 4 |
I know how to pull the post info together with the author's id:
SELECT `id`, `post_title`, `post_content`, `post_status`, `meta_value` FROM `wp_posts`
LEFT JOIN wp_postmeta
ON `id` = `post_id`
WHERE `post_type` = 'post' AND `post_status` in ('trash','publish') AND `meta_key` = 'visitor_tracking_visitor_id'
如何仅使用SQL(没有Wordpress函数)将作者发布的帖子数量拉到另一个字段?
在此先感谢您的帮助!
2 回答
为什么要为每个帖子中已有的内容创建post meta?只需获取帖子的作者ID get_the_author_id()
如果你想要一个作者的列表发布链接,在循环中你可以使用https://codex.wordpress.org/Function_Reference/the_author_posts_link
要获得作者或用户发布计数,您可以使用https://codex.wordpress.org/Function_Reference/count_user_posts
不需要为此使用SQL:
了解更多关于WP_Query in the Codex的信息 .