【问题标题】:Pushing Wordpress to its limits with advanced meta query filtering通过高级元查询过滤将 Wordpress 推向极限
【发布时间】:2020-01-21 09:16:57
【问题描述】:

情况就是这样,我们已经建立了一个相当复杂的 Wordpress 网站,我们的用户注册并登录以访问网站上的页面,多种帖子类型中的页面和资产都具有分类法和分配给他们的自定义元字段管理用户是否应该有权访问页面/资产。

这是我当前正在运行的设置:

公司的自定义帖子类型

通过高级自定义字段字段为用户分配一个公司对象。

每个公司都分配有一个“类型”分类。

网站上使用的每个页面和其他自定义帖子也都有此分类,用于过滤哪些公司类型在浏览网站时应该看到哪些内容。

对于一种帖子类型,我们使用此分类过滤器 (tax_query),但也使用元查询,因为我们还可以从公司帖子类型中选择特定公司来查看某些帖子。这目前正在使用以下用于过滤搜索结果查询的函数(例如):

/* Filter the search results by company type */
function filter_search_results($query){
    if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_search()) {
            $filter = array();
            $companytype = get_current_user_companytype(); // custom function to grab type taxonomy from current users company
            $vfuser = get_user_details(); // custom function to get current users details
            $companyid = intval($vfuser['companyid']); // get the users company id
            if ($companyid){
                $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', 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' => '='
                    )
            ) );
            $query->set( 'meta_query', $meta_query );
            $query->set( 'posts_per_page', 24 );
        }
    }
}
add_action( 'pre_get_posts', 'filter_search_results' );

现在,我想做的是更进一步,允许特定用户过滤资产,然后覆盖上述内容,仅向为该帖子选择的用户显示资产。

我创建了一个自定义帖子元字段和一个表单,其中列出了网站上的所有用户,这正在工作并将一组用户 ID 提交到可以检查每个帖子的字段中,但元查询变为如此复杂以至于导致 504 超时。以下是我写的查询,有没有人有任何建议或更简单的方法,不胜感激。

/* Filter the search results by company type */
function filter_search_results($query){
    if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_search()) {
            $filter = array();
            $companytype = get_current_user_companytype(); // custom function to grab type taxonomy from current users company
            $vfuser = get_user_details(); // custom function to get current users details
            $companyid = intval($vfuser['companyid']); // get the users company id
            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', array(
                array(
                    // Users exist
                    'relation' => 'AND',
                    array(
                      'key' => 'comms_users',
                      'compare' => 'EXISTS'
                    ),
                    array(
                        'key' => 'comms_users',
                        'value' => serialize(strval(get_current_user_id())),
                        'compare' => 'LIKE',
                    ),
                ),
                array(
                    // Companies exist but Users do not
                    'relation' => 'AND',
                    array(
                        'relation' => 'OR',
                        array(
                          'key' => 'comms_users',
                          'compare' => 'NOT EXISTS'
                        ),
                        array(
                          'key'     => 'comms_users',
                          'value'   => false,
                          'compare' => '='
                        )
                    ),
                    array(
                        'key' => 'company_select',
                        'value' => '"' . $companyid . '"',
                        'compare' => 'LIKE',
                    ),
                ),
                array(
                    // Company and User select do not exist
                    'relation' => 'AND',
                    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' => '='
                        )
                    )
                )
            ) );
            $query->set( 'meta_query', $meta_query );
            $query->set( 'posts_per_page', 24 );
        }
    }
}
add_action( 'pre_get_posts', 'filter_search_results' );

【问题讨论】:

    标签: php wordpress advanced-custom-fields meta-query


    【解决方案1】:

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

    使用的最终代码如下:

    /* 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' );
    

    【讨论】:

      猜你喜欢
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多