如何根据作为查询字符串参数传递的user_id过滤admin中的自定义帖子类型列表中的记录

首先,我想对我在WordPress开发中的糟糕知识表示歉意。如何根据作为查询字符串参数传递的user_id过滤admin中的自定义帖子类型列表中的记录

是否有任何选项,我可以进一步过滤管理员基于user_id的自定义帖子类型的帖子列表,如果相同已被传递为查询字符串,如http://album.multibaselocal.com/wp-admin/edit.php?post_type=albums&user_id=31

这是我想要实现的: 我有一个自定义的帖子类型,名为album,网站用户使用它来上传相册到系统。默认情况下,当我进入列表页面时,所有相册都可以正常使用。在用户列表页面中,我添加了一个自定义列,显示每个用户链接回专辑列表页面的已发布专辑数,查询字符串为user_id,如上所示。

我想通过user_id过滤相册我已通过。这可能吗?

我想这一点:

add_filter('parse_query', 'filter_albums_further_if_user_id_found'); 

function filter_albums_further_if_user_id_found($query) {

if(isset($_GET['user_id']) && !empty($_GET['user_id'])) {

$user_id = $_GET['user_id'];

$requery = array(

'post_type' => 'albums',

'post_author' => $user_id,

'status' => 'publish',

'posts_per_page' => -1,

);

// What to do here?

}

}

我在这里完全丧失。不是有经验的WP开发人员!

如果我没有明白这一切都是错误的,因为我使用的钩子被称为parse_query,我不得不以某种方式再次解析查询并使用user_id筛选现有结果。但不知道在哪里以及如何:(

您的帮助将不胜感激

EDIT(查询输出):

WP_Query Object 

(

[query] => Array

(

[post_type] => albums

[posts_per_page] => 20

)

[query_vars] => Array

(

[post_type] => albums

[posts_per_page] => 20

[error] =>

[m] =>

[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] =>

[author_name] =>

[feed] =>

[tb] =>

[paged] => 0

[meta_key] => user_id

[meta_value] => 31

[preview] =>

[s] =>

[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] => 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

(

)

[author__in] => Array

(

)

[author__not_in] => Array

(

)

)

[tax_query] => WP_Tax_Query Object

(

[queries] => Array

(

)

[relation] => AND

[table_aliases:protected] => Array

(

)

[queried_terms] => Array

(

)

[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] => 1

[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_embed] =>

[is_paged] =>

[is_admin] => 1

[is_attachment] =>

[is_singular] =>

[is_robots] =>

[is_posts_page] =>

[is_post_type_archive] => 1

[query_vars_hash:WP_Query:private] => 1c0b485390fc9607d8008a14177bd63d

[query_vars_changed:WP_Query:private] =>

[thumbnails_cached] =>

[stopwords:WP_Query:private] =>

[compat_fields:WP_Query:private] => Array

(

[0] => query_vars_hash

[1] => query_vars_changed

)

[compat_methods:WP_Query:private] => Array

(

[0] => init_query_flags

[1] => parse_tax_query

)

)

EDIT(函数体目前正在使用):

add_action('pre_get_posts', 'filter_albums_further_if_user_id_found'); 

function filter_albums_further_if_user_id_found($query) {

if(!is_admin())

return;

if(isset($_GET['user_id']) && !empty($_GET['user_id'])) {

$user_id = $_GET['user_id'];

$query->set('meta_key', 'user_id');

$query->set('meta_value', $user_id);

}

echo '<pre>'; print_r($query); echo '</pre>'; die();

}

回答:

这让我想起了几周前的一个类似的问题。 ng来调整'orderby'部分的查询,但我相信你可以用同样的动作完成你的任务。尝试使用“pre_get_posts”操作而不是该过滤器。我没有测试过,所以让我知道它是否有效!

正确答案(用户输入之后EDITED):

function user_id_filter($query) { 

if (! is_admin() && !$query->is_main_query())

return;

if(isset($_GET['user_id']) && !empty($_GET['user_id'])) {

$user_id = $_GET['user_id'];

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

}

//Debug if necessary

//echo '<pre>'; print_r($query); echo '</pre>'; die();

}

add_action('pre_get_posts', 'user_id_filter', 500000000);

我用这作为参考。 https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

以上是 如何根据作为查询字符串参数传递的user_id过滤admin中的自定义帖子类型列表中的记录 的全部内容, 来源链接: utcz.com/qa/263852.html

回到顶部