【问题标题】:Using pre_get_posts to build custom query not working使用 pre_get_posts 构建自定义查询不起作用
【发布时间】:2015-05-09 05:58:24
【问题描述】:

我一直在努力让它发挥作用,并查看了所有看起来应该的东西。

我有一个页面,我在其中添加了一个自定义排序函数,该函数使用 pre_get_posts 和 set_query_var 更改 WP Query

简而言之,我有几个选择框,用户可以在其中选择一些东西来过滤循环。例如,一旦用户提交查询,我的网址将如下所示:http://my-domain.com/gallery/?classroom=&digital_items=&kits_used=26155&project_type=&post_author=

所以我添加了一个帖子,其中包含 kits_used 的帖子元并添加了一些“工具包”,检查我的数据库中的 postmeta 并查看 meta_value 的数组应该是这样的:a:2:{i:0;s:5:"26155";i:1;s:5:"26152";}

但是,我觉得我的 pre_get_posts 函数有问题,因为当我选择 id 为 26155 的工具包时,它说找不到帖子。不确定我是否做得正确,因为它是一个数组,或者我在这里遗漏了其他东西。

我的功能:

add_action('pre_get_posts', 'gallery_sort_query');

function gallery_sort_query( $query )
{
// validate
    if( is_admin() )
    {
        return;
    }

    if( !$query->is_main_query() )
    {
        return;
    }

// get original meta query
    $meta_query = $query->get('meta_query');

    // allow the url to alter the query
    if( !empty($_GET['kits_used']) )
    {
        $kits_used = explode(',', $_GET['kits_used']);

        //Add our meta query to the original meta queries
        $meta_query[] = array(
            'key'       => 'kits_used',
            'value'     => $kits_used,
            'compare'   => 'IN',
            );
    }

    if( !empty($_GET['project_type']) )
    {
        $project_type = explode(',', $_GET['project_type']);

        //Add our meta query to the original meta queries
        $meta_query[] = array(
            'key'       => 'project_type',
            'value'     => $project_type,
            'compare'   => 'IN',
            );
    }

    if( !empty($_GET['digital_items']) )
    {
        $digital_items = explode(',', $_GET['digital_items']);

        //Add our meta query to the original meta queries
        $meta_query[] = array(
            'key'       => 'digital_items',
            'value'     => $digital_items,
            'compare'   => 'IN',
            );
    }

    if( !empty($_GET['classroom']) )
    {
        $classroom = explode(',', $_GET['classroom']);

        //Add our meta query to the original meta queries
        $meta_query[] = array(
            'key'       => 'classroom',
            'value'     => $classroom,
            'compare'   => 'IN',
            );
    }

    if( !empty($_GET['post_author']) )
    {
        $author = $_GET['post_author'];



        $query->set( 'author' , $author );

    }

// update the meta query args
    $query->set('meta_query', $meta_query);

// always return
    return;

}

如果我遗漏了什么,真的可以在这里使用一些帮助,一切看起来都在排队,但由于某种原因,除了“作者”排序下拉菜单外,它仍然无法正常工作。希望我提供的信息太多而不是不够:)

【问题讨论】:

    标签: php mysql wordpress


    【解决方案1】:

    对于寻找类似内容的其他人,我找到了答案。因为元值作为数组存储在数据库中,所以我需要使用 LIKE 来比较那些特定的查询。

    这是完整的工作函数,请注意我在哪里使用 LIKE 而不是 IN 来表示作为数组的 meta_values。

    add_action('pre_get_posts', 'gallery_sort_query');
    
    function gallery_sort_query( $query )
    {
    // validate
        if( is_admin() )
        {
            return;
        }
    
        if( !$query->is_main_query() )
        {
            return;
        }
    
    // get original meta query
        $meta_query = $query->get('meta_query');
    
        // allow the url to alter the query
        if( !empty($_GET['kits_used']) )
        {
            $kits_used = explode(',', $_GET['kits_used']);
    
            //Add our meta query to the original meta queries
            $meta_query[] = array(
                'key'       => 'kits_used',
                'value'     => $kits_used,
                'compare'   => 'LIKE',
                );
        }
    
        if( !empty($_GET['project_type']) )
        {
            $project_type = explode(',', $_GET['project_type']);
    
            //Add our meta query to the original meta queries
            $meta_query[] = array(
                'key'       => 'project_type',
                'value'     => $project_type,
                'compare'   => 'IN',
                );
        }
    
        if( !empty($_GET['digital_items']) )
        {
            $digital_items = explode(',', $_GET['digital_items']);
    
            //Add our meta query to the original meta queries
            $meta_query[] = array(
                'key'       => 'digital_items',
                'value'     => $digital_items,
                'compare'   => 'LIKE',
                );
        }
    
        if( !empty($_GET['classroom']) )
        {
            $classroom = explode(',', $_GET['classroom']);
    
            //Add our meta query to the original meta queries
            $meta_query[] = array(
                'key'       => 'classroom',
                'value'     => $classroom,
                'compare'   => 'IN',
                );
        }
    
        if( !empty($_GET['post_author']) )
        {
            $author = $_GET['post_author'];
    
    
    
            $query->set( 'author' , $author );
    
        }
    
    // update the meta query args
        $query->set('meta_query', $meta_query);
    
    // always return
        return;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多