dongpang1898 2013-04-12 17:13
浏览 71
已采纳

wordpress posts_orderby在插件中使用自定义表进行过滤

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.

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).

The custom table have these values:

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

and it works.

So there's something wrong in the code written in my plugin file but I can't figure it out.

  • 写回答

1条回答 默认 最新

  • dongrou5254 2013-04-13 06:41
    关注

    Ok, I solved it.

    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;
    }
    

    I added also the posts_groupby filter because the new query gave me duplicated posts (lots of duplicated posts).

    Here's the code:

    add_filter('posts_groupby','custom_groupby');
    
    function custom_groupby($groupby){
        global $wpdb;
    
        if(!is_admin){
           $groupby = "$wpdb->posts.ID";
        }
    
        return $groupby;
    }
    

    Everything is written in the plugin file, but you can write also in the function.php file of your theme.

    Remember to include the if(!is_admin) statement if you want to see the custom query only on front end.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。