如何将UNION SQL查询附加到默认搜索查询

时间:2014-09-19 作者:Mahesh

示例:

(SELECT ID, post_status, post_title, post_excerpt, post_content 
FROM wp_posts 
WHERE ((`post_title` LIKE \'%2020%\') OR (`post_excerpt` LIKE \'%2020%\')
OR (`post_content` LIKE \'%2020%\')) 
AND (`post_status` = \'publish\' )
)
UNION (SELECT ID, \'\', title, \'\', content 
FROM wp_software_and_hardware_post 
WHERE (`title` LIKE \'%2020%\') OR (`content` LIKE \'%2020%\')
and status=1)
ORDER BY `post_status` DESC;
比如我可以使用哪种过滤方法。

1 个回复
最合适的回答,由SO网友:birgire 整理而成

将数据附加到本机搜索:

您可以尝试,例如:

add_action( \'pre_get_posts\', function( $q ) {
    if( $q->is_main_query() && $q->is_search() )
    {
        $q->set( \'nopaging\', true );
        add_filter( \'posts_request\', function( $request ) {
            $sql = \'YOUR EXTRA SQL QUERY\';
            return "({$request}) UNION ({$sql}) ORDER BY post_status DESC";
        });  
    }
});
其中,我们使用无分页版本来匹配您的SQL查询。

重用本机搜索查询:

如果我正确理解了您的注释,那么您希望在额外的SQL查询中重用主搜索查询。这里有一个想法:

/**
 * Add data to the native WordPress search
 *
 * @see http://wordpress.stackexchange.com/a/161949/26350
 */
add_action( \'pre_get_posts\', function( $q ) {
    if( $q->is_main_query() && $q->is_search() )
    {
        $q->set( \'nopaging\', true );
        $c = new WPSE_Add_Search_Data;
        $c->init();
    }
});
其中WPSE_Add_Search_Data 类别定义为:

/**
 * class WPSE_Add_Search_Data
 */
class WPSE_Add_Search_Data
{
    protected $search_where = \'\';

    public function init()
    {
        add_filter( \'posts_search\',  array( $this, \'posts_search\'  ) );
        add_filter( \'posts_fields\',  array( $this, \'posts_fields\'  ) );
        add_filter( \'posts_request\', array( $this, \'posts_request\' ) );
        add_filter( \'posts_orderby\', \'__return_null\' );
    }

    public function posts_search( $search )
    {
        $this->search_where = $search;
        return $search;
    }

    public function posts_fields( $fields )
    {
        return \'ID, post_title, post_content, post_status\';
    }

    public function posts_request( $request )
    {
        global $wpdb;

        $search_where = str_ireplace( 
            array( $wpdb->posts . ".post_", "AND (password = \'\')" ), 
            "", 
            $this->search_where 
        );

        // Fetch data from the external table.
        // The fields much match the fields from the main search query.
        $sql = "SELECT ID, title as post_title, 
                       content as post_content, status as post_status
                FROM {$wpdb->prefix}software_and_hardware_post
                WHERE 1=1 {$search_where} AND status=1";

        // Append the external data with custom order:
        return  "({$request}) UNION ({$sql}) ORDER BY post_status DESC";
    }

} // end class

结束

相关推荐

Use search in fancybox

我正在尝试使用FancyBox打开一个lightbox样式的弹出窗口searchform.php, 当用户单击搜索图标时。The setup:英寸header.php 我有一个链接元素,它将只显示searchicon英寸script.js 我将点击搜索图标绑定到fancybox,并打开模式/灯箱iframe 内容为searchform.php;The problem: 这个iframe 未加载WordPress环境,因此无法正常工作。如何使用fancybox并仍然可以访问WP功能?下一步将以相同的方式显示