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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器