1

如果已将相同的查询字符串作为查询字符串传递,是否有任何选项可以根据 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. 但不知道在哪里以及如何。

编辑(查询输出):

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
        )

)

编辑(目前使用的功能体):

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();
}
4

1 回答 1

1

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

正确答案(用户输入后编辑):

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

于 2017-12-18T16:26:35.060 回答