通过 Wordpress 中的存储过程获取帖子

Get posts via stored procedure in Wordpress

我有一个存储过程,它 return 是一个数据集,特定于输入参数。 当我尝试从 Wordpress 调用我的过程时,如下所示,我得到了类似

的错误

WordPress 数据库错误:[命令不同步;你现在不能运行这个命令]

add_filter('posts_request', 'search_posts_request', 2, 10 );

function search_posts_request($posts, $wpquery) {
   return "call my_sp(1, 0)";
}

你能给我建议吗(临时 table 解决方案除外,因为 sp 的结果数据集存储),如何解决这个问题和 return 来自 sp 的数据?

我找到了解决方案:

  1. 在 wp-config.php 添加这一行以启用 mysqli 而不是 mysql lib:

    define( 'WP_USE_EXT_MYSQL', false);
    
  2. 在 functions.php 我更新了我的代码如下。我添加了一个标志来标记存储过程调用,以便调用 mysqli_next_result() 将响应缓冲区清除为 sp returns 两个结果(数据集和结果代码)而不是一个:

    $sp_called = false;
    
    add_filter('posts_request', 'search_posts_request', 2, 10 );
    add_filter('found_posts_query', 'search_found_posts_query', 2, 10 );
    
    
    function search_posts_request($posts, $wpquery) 
    {
        // mark sql request as a sp call
        $sp_called = true;
        return "call my_sp(1, 0)";
    }
    
    public function search_found_posts_query($query, $wpquery) 
    {
        global $wpdb, $sp_called;
    
        if ($sp_called) 
        {
            // clear last response and reset sp call flag
            mysqli_next_result($wpdb->dbh);
            $sp_called = false;
        }
    
        return $query;
    }