gpt4 book ai didi

php - WordPress:多站点帖子查询的过滤器类别

转载 作者:行者123 更新时间:2023-11-29 23:49:09 25 4
gpt4 key购买 nike

我的情况

首先,我之前没有做过任何自定义 SQL 查询,所以请耐心等待..

我有一个包含多个博客的多站点安装。以下函数查询所有博客的最新帖子,效果很好。

工作代码,取自https://gist.github.com/mhulse/5718743

function recent_ms_posts($count = 10, $cat, $ignore = array('')) {

global $wpdb, $table_prefix;

$ignore = implode(', ', array_map('absint', $ignore));
$rows = NULL;
$tables = array();
$query = '';
$i = 0;
$posts = array();
$post = NULL;

$rows = $wpdb->get_results($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE blog_id NOT IN ($ignore) AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'", 0), ARRAY_A);

if ($rows) {

foreach ($rows as $row) {
$tables[$row['blog_id']] = $wpdb->get_blog_prefix($row['blog_id']) . 'posts';
}

if (count($tables)) {
foreach ($tables as $blog_id => $table) {
if ($i)
$query .= ' UNION ';

$query .= " (SELECT ID, post_date, $blog_id as `blog_id` FROM $table WHERE post_status = 'publish' AND post_type = 'post')";
$i++;
}

$query .= " ORDER BY post_date DESC LIMIT 0, $count;";
$rows = $wpdb->get_results($query);

if ($rows) {
foreach ($rows as $row) {
$post = get_blog_post($row->blog_id, $row->ID);
$post->blog_id = $row->blog_id;
$post->row_id =$row->ID;
$post->permalink = get_blog_permalink($row->blog_id, $row->ID);
$posts[] = $post;
}

return $posts;
}
}
}
}
<小时/>

我的问题

我现在想做的是过滤掉某个类别的帖子,我尝试稍微修改一下查询字符串:

修改后的代码不起作用

$query .= " (SELECT ID, post_date, $blog_id as `blog_id` FROM $table WHERE post_status = 'publish' AND post_type = 'post' AND term_taxonomy.taxonomy = 'category' AND terms.slug = '$cat')";
<小时/>

我的问题

如何使 term_taxonomy.taxonomy = 'category'terms.slug = '$cat' 按我希望的方式工作?

最佳答案

好的,所以我通过更改 $query 字符串并添加一些变量解决了问题:

$table_posts = $table . 'posts';
$table_term_relationships = $table . 'term_relationships';
$table_term_taxonomy = $table . 'term_taxonomy';
$table_terms = $table . 'terms';

if ($i)
$query .= ' UNION ';

$query .= " (
SELECT
$table_posts.ID,
$table_posts.post_date,
$blog_id as `blog_id`
FROM
$table_posts
LEFT JOIN $table_term_relationships ON ($table_posts.ID = $table_term_relationships.object_id)
LEFT JOIN $table_term_taxonomy ON ($table_term_relationships.term_taxonomy_id = $table_term_taxonomy.term_taxonomy_id)
LEFT JOIN $table_terms ON ($table_term_relationships.term_taxonomy_id = $table_terms.term_id)
WHERE
post_status = 'publish'
AND
post_type = 'post'
AND
$table_term_taxonomy.taxonomy = 'category'
AND
$table_terms.slug LIKE '$cat'
)";

关于php - WordPress:多站点帖子查询的过滤器类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25741089/

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