WordPress的posts_orderby过滤器与插件中的自定义表 [英] wordpress posts_orderby filter with custom table in plugin
问题描述
我正在开发一个Wordpress插件,需要使用我用该插件创建的自定义表格对网站的帖子进行排序.
I'm developing a Wordpress plugin and need to order the posts of the site with the custom table that I created with my plugin.
我不想更改主题中的代码,所以我在法典上找到了过滤器posts_orderby
和posts_join
(在此处找到: https://codex.wordpress.org/Custom_Queries ).
I don't want to alter the code within the theme so I found on codex the filters posts_orderby
and posts_join
(found here: https://codex.wordpress.org/Custom_Queries).
自定义表具有以下值:
ID slug price
,并在我添加以下行的插件文件中:
and in the plugin file where I added these lines:
add_filter('posts_orderby','custom_orderby');
add_filter('posts_join','custom_join');
function custom_join($join){
global $wpdb;
$customTable = $wpdb->prefix.'custom_table';
if(!is_admin()){
$join .= " LEFT JOIN $customTable ON $wpdb->postmeta.meta_value = $customTable.slug";
}
return $join;
}
function custom_orderby($orderby_statement){
global $wpdb;
$customTable = $wpdb->prefix.'custom_table';
if(!is_admin()){
$orderby_statement = "$customTable.price DESC";
}
return $orderby_statement;
}
当我刷新索引页面时,它会显示以下错误消息:
When I refresh the index page it gives me this error message:
No Results Found
The page you requested could not be found. Try refining your search, or use the navigation above to locate the post.
我尝试使用以下代码直接在数据库中进行查询:
I tried to do the query directly on my database with this code:
SELECT * FROM wp_posts t1
LEFT JOIN wp_postmeta t2 ON t1.ID = t2.post_id
LEFT JOIN wp_custom_table t3 ON t2.meta_value = t3.slug
ORDER BY t3.price DESC
它有效.
所以我的插件文件中编写的代码出了点问题,但我无法弄清楚.
So there's something wrong in the code written in my plugin file but I can't figure it out.
推荐答案
好,我解决了.
问题在于发布查询不包含postmeta表,因此我将其添加到custom_join
函数中,如下所示:
The problem is that the post query doesn't include the postmeta table, so I added it on the custom_join
function, like this:
add_filter('posts_join','custom_join');
add_filter('posts_orderby','custom_orderby');
function custom_join($join){
global $wpdb;
$customTable = $wpdb->prefix."custom_table";
if(!is_admin){
$join .= "LEFT JOIN $wpdb->postmeta p1 ON $wpdb->posts.ID = p1.post_id";
$join .= "LEFT JOIN $customTable p2 ON p1.meta_value = p2.slug";
}
return $join;
}
function custom_orderby($orderby_statement){
global $wpdb;
if(!is_admin){
$orderby_statement = "p2.price DESC, $wpdb->posts.post_date DESC";
}
return $orderby_statement;
}
我还添加了posts_groupby
过滤器,因为新查询为我提供了重复的帖子(很多重复的帖子).
I added also the posts_groupby
filter because the new query gave me duplicated posts (lots of duplicated posts).
代码如下:
add_filter('posts_groupby','custom_groupby');
function custom_groupby($groupby){
global $wpdb;
if(!is_admin){
$groupby = "$wpdb->posts.ID";
}
return $groupby;
}
一切都写在插件文件中,但您也可以写在主题的function.php
文件中.
Everything is written in the plugin file, but you can write also in the function.php
file of your theme.
如果要在前端仅看到自定义查询,请记住包含if(!is_admin)
语句.
Remember to include the if(!is_admin)
statement if you want to see the custom query only on front end.
这篇关于WordPress的posts_orderby过滤器与插件中的自定义表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!