请确保我在使用Pre_Get_Post时没有发疯

时间:2013-10-21 作者:Robert Ruby II

 function rpm_pre_get_post($query){
       if($query->is_main_query()){
            $query->set(\'meta_key\', \'parent-project\');
            $query->set(\'meta_value\', \'Metro-E\');
        }
}
add_action( \'pre_get_posts\', \'rpm_pre_get_post\', 9999 );
这应该会有效地撤回所有关键“父项目”设置为“Metro-e”的帖子,对吗?

下面是我在调试时得到的结果

WP_Query Object
(
    [query_vars] => Array
        (
            [post_type] => rfd_job
            [posts_per_page] => 20
            [error] => 
            [m] => 0
            [p] => 0
            [post_parent] => 
            [subpost] => 
            [subpost_id] => 
            [attachment] => 
            [attachment_id] => 0
            [name] => 
            [static] => 
            [pagename] => 
            [page_id] => 0
            [second] => 
            [minute] => 
            [hour] => 
            [day] => 0
            [monthnum] => 0
            [year] => 0
            [w] => 0
            [category_name] => 
            [tag] => 
            [cat] => 
            [tag_id] => 
            [author_name] => 
            [feed] => 
            [tb] => 
            [paged] => 0
            [comments_popup] => 
            [meta_key] => parent-project
            [meta_value] => Metro-E
            [preview] => 
            [s] => 
            [sentence] => 
            [fields] => 
            [menu_order] => 
            [category__in] => Array
                (
                )

            [category__not_in] => Array
                (
                )

            [category__and] => Array
                (
                )

            [post__in] => Array
                (
                )

            [post__not_in] => Array
                (
                )

            [tag__in] => Array
                (
                )

            [tag__not_in] => Array
                (
                )

            [tag__and] => Array
                (
                )

            [tag_slug__in] => Array
                (
                )

            [tag_slug__and] => Array
                (
                )

            [post_parent__in] => Array
                (
                )

            [post_parent__not_in] => Array
                (
                )

        )

    [tax_query] => WP_Tax_Query Object
        (
            [queries] => Array
                (
                )

            [relation] => AND
        )

    [meta_query] => 
    [post_count] => 0
    [current_post] => -1
    [in_the_loop] => 
    [comment_count] => 0
    [current_comment] => -1
    [found_posts] => 0
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] => 
    [is_preview] => 
    [is_page] => 
    [is_archive] => 
    [is_date] => 
    [is_year] => 
    [is_month] => 
    [is_day] => 
    [is_time] => 
    [is_author] => 
    [is_category] => 
    [is_tag] => 
    [is_tax] => 
    [is_search] => 
    [is_feed] => 
    [is_comment_feed] => 
    [is_trackback] => 
    [is_home] => 
    [is_404] => 
    [is_comments_popup] => 
    [is_paged] => 
    [is_admin] => 1
    [is_attachment] => 
    [is_singular] => 
    [is_robots] => 
    [is_posts_page] => 
    [is_post_type_archive] => 
    [query_vars_hash] => cf1479fdf40685bcc6b8eb6b842a2f6b
    [query_vars_changed] => 
    [thumbnails_cached] => 
    [query] => Array
        (
            [post_type] => rfd_job
            [posts_per_page] => 20
        )

2 个回复
最合适的回答,由SO网友:Robert Ruby II 整理而成

我所做的搜索筛选导致了一个问题。我删除了那个代码,一切都很好。

SO网友:tatemz

也许你应该试试这个:

function rpm_pre_get_post($query){
   if($query->is_main_query()){
        $query->set(\'meta_query\', array(
               array(
                   \'key\' => \'parent-project\',
                   \'value\' => \'Metro-E\'
               )
           )
        );
    }
}
add_action( \'pre_get_posts\', \'rpm_pre_get_post\', 9999 );

结束

相关推荐