我正在开发一个WordPress插件,我在其中创建了自定义帖子,并使用元数据将它们链接在一起。
在管理方面,我希望能够使用这些关系过滤帖子列表。我想我需要使用INNER JOIN,但我不知道如何使用。
目前,我只有:
add_filter( 'parse_query', 'mypostsfilteringfunction' );
function mypostsfilteringfunction( $query ){
// if post_type matches :
// then I need to be able to add innerjoin into $query
}
A通过做来实现我的目标
add_filter( 'parse_query', 'mypostsfilteringfunction' );
function mypostsfilteringfunction( $query ){
// if post_type matches :
add_filter( 'posts_join', 'myplugin_addjointorequest' );
add_filter( 'posts_where', 'myplugin_addwheretorequest' );
add_filter( 'posts_request', 'myplugin_sqlprint' ); // for printing result
}
function myplugin_addjointorequest ($join){
$join .= 'INNER JOIN ...';
return $join;
}
function myplugin_addwheretorequest ($where){
$where .= 'AND ...';
return $where;
}
function myplugin_sqlprint( $request ) {
echo $request;
return $request;
}