wordpress $ wpdb->前缀不起作用

时间:2018-05-04 09:24:34

标签: php wordpress function plugins

我已经获得了Relevanssi支持人员的下面代码,但我需要针对多站点设置进行调整。

第一段代码是我发送的内容的原件,我保证可以正常工作;第二块代码是我尝试转换为多站点使用的原因。

问题是我无法上班!两者都导致索引跳过所有帖子,但我需要检查我的多站点设置是否正确,因为我对这些查询不太熟悉......

add_filter( 'relevanssi_indexing_restriction', 'rlv_hidden_cats' );
function rlv_hidden_cats( $restriction ) {
    global $wpdb;
    $restriction .= " AND post.ID NOT IN (SELECT object_id FROM $wpdb->term_relationships AS tr, $wpdb->term_taxonomy AS tt WHERE tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.parent NOT IN (13, 14, 15)) ";
    return $restriction;
}

...

add_filter( 'relevanssi_indexing_restriction', 'rlv_hidden_cats' );
function rlv_hidden_cats( $restriction ) {
    global $wpdb;
    $restriction .= " AND post.ID NOT IN (SELECT object_id FROM $wpdb->prefix->term_relationships AS tr, $wpdb->prefix->term_taxonomy AS tt WHERE tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.parent NOT IN (13)) ";
    return $restriction;
}

我的更改发生在" $ restriction"线

1 个答案:

答案 0 :(得分:2)

对于多站点,如果它不是根网站,您可以使用$wpdb->get_blog_prefix($blog_id)

获取前缀

然后,你可以做

$base_prefix = $wpdb->get_blog_prefix($123);
$restriction .= " AND post.ID NOT IN (SELECT object_id FROM {$base_prefix}term_relationships AS tr, {$base_prefix}term_taxonomy AS tt WHERE tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.parent NOT IN (13, 14, 15)) ";

如果是根网站,您可以使用$wpdb->base_prefix

$restriction .= " AND post.ID NOT IN (SELECT object_id FROM {$wpdb->base_prefix}term_relationships AS tr, {$wpdb->base_prefix}term_taxonomy AS tt WHERE tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.parent NOT IN (13, 14, 15)) ";

如果它是多站点的当前网站:

$restriction .= " AND post.ID NOT IN (SELECT object_id FROM {$wpdb->prefix}term_relationships AS tr, {$wpdb->prefix}term_taxonomy AS tt WHERE tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.parent NOT IN (13, 14, 15)) ";

P.S。 $wpdb->prefix$wpdb->base_prefix字符串,您不能$wpdb->prefix->term_relationships。但是,您可以执行类似$wpdb->term_relationships的操作,它将为您提供表term_relationships的全名(例如,如果前缀为wp_,则您将获得wp_term_relationships)