在wp-admin中使用pre_get_posts

时间:2019-04-05 01:01:46

标签: wordpress function search admin custom-post-type

我具有“设备”的自定义帖子类型以及“序列号”的元键。我想查询wp-admin中的序列postmeta字段。

我在https://mydomain/wp-admin/edit.php?post_type=equipment上设置了一个“自定义”搜索页面

所有这些都工作正常,问题是,我希望它默认查询序列/模型后置。我的自定义功能似乎无效。它确实测试正确,因为我正在将$ query数组打印到屏幕上。

add_action( 'pre_get_posts', 'admin_search_meta' );

function admin_search_meta( $query ) {

$type = get_query_var('post_type');
if (  $query->is_main_query() && is_admin() && $type == "equipment") {


        //$query->set( 'posts_per_page', '-1' );

        $meta_query_args = array(
                'relation' => 'AND',
                array(
                    'key'     => 'serial',
                    'value'   => get_query_var('s'),
                    'compare' => 'REGEXP'
                ),

                array(
                    'key'     => 'model',
                    'value'   => get_query_var('s'),
                    'compare' => 'REGEXP'
                )

            );
        $query->set( 'meta_query', $meta_query_args );

print_r($query);

}

}

这是print_r($ query);

的输出
  

WP_Query对象([查询] =>数组([m] => 0 [s] => Z452514A [分页] => 1 [命令] => [命令] => [post_type] =>设备[posts_per_page] => 200 [post_status] => [perm] =>)[query_vars] =>数组([m] => 0 [s] => Z452514A [paged] => 1 [order] => [orderby] => [ post_type] =>设备[posts_per_page] => 200 [post_status] => [perm] => [错误] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [附件] => [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] => [author_name] => [feed ] => [tb] => [meta_key] => [meta_value] => [preview] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in ] => Array()[category__not_in] => Array()[category__and] => Array()[post__in] => Array()[post__not_in] => Array()[post_name__in] => Array()[tag__in] = >数组()[tag__not_in] =>数组() [tag__and] => Array()[tag_slug__in] => Array()[tag_slug__and] => Array()[post_parent__in] => Array()[post_parent__not_in] => Array()[author__in] => Array()[author__not_in ] => Array()[meta_query] => Array([relation] => AND [0] => Array([key] => serial [value] => Z452514A [compare] => REGEXP)[1] => Array([key] => engine [value] => Z452514A [compare] => REGEXP)))[tax_query] => WP_Tax_Query Object([queries] => Array()[relation] => AND [table_aliases:protected] =>数组()[查询的条件] =>数组()[primary_table] => [primary_id_column] =>)[meta_query] => [date_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 ] => 1 [is_feed] => [ is_comment_feed] => [is_trackback] => [is_home] => [is_404] => [is_embed] => [is_paged] => [is_admin] => 1 [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => d36484fc4412f4eec2df43a812ee8d7a [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [停止字词: =>数组([0] => query_vars_hash [1] => query_vars_changed)[compat_methods:WP_Query:private] =>数组([0] => init_query_flags [1] => parse_tax_query))

0 个答案:

没有答案