0
4

1 回答 1

0

以防万一它对将来的任何人有用(我知道这是一个非常定制的请求,但你永远不知道)我设法找到了一个查询,该解决方案在哪里工作并且在服务器上不是太密集。

使用的最终代码如下:

/* Filter the search results by partner type */
function filter_search_results($query){
    if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_search()) {
            $filter = array();
            $companytype = get_current_user_ companytype();
            $vfuser = get_user_details();
            $companyid = intval($vfuser['companyid']);
            if ($companytype){
                $filter[] = array('taxonomy' => 'companytype', 'field' => 'name', 'terms' => $companytype);
            }
            $tax_query = array('relation' => 'AND', $filter);
            $query->set( 'tax_query', $tax_query );
            $meta_query = array('relation' => 'OR',
                // Company exists and no users selected
                array(
                    'relation' => 'AND',
                    array(
                        'relation' => 'OR',
                        array(
                            'key' => 'company_select',
                            'value' => '"' . $companyid . '"',
                            'compare' => 'LIKE',
                        ),
                        array(
                            'relation' => 'OR',
                            array(
                                'key' => 'company_select',
                                'compare' => 'NOT EXISTS'
                            ),
                            array(
                                'key'     => 'company_select',
                                'value'   => false,
                                'compare' => '='
                            )
                        )
                    ),
                    array(
                        'relation' => 'OR',
                        array(
                            'key' => 'comms_users',
                            'compare' => 'NOT EXISTS'
                        ),
                        array(
                            'key' => 'comms_users',
                            'value' => false,
                            'compare' => '='
                        )
                    )
                ),
                // Users are selected
                array(
                    'key' => 'comms_users',
                    'value' => serialize(strval(get_current_user_id())),
                    'compare' => 'LIKE'
                )
            );
            $query->set( 'meta_query', $meta_query );
            $query->set( 'posts_per_page', 24 );
        }
    }
}
add_action( 'pre_get_posts', 'filter_search_results' );
于 2020-02-06T08:47:54.517 回答