Wordpress管理员:在自定义帖子类型

时间:2017-10-25 07:46:29

标签: php wordpress

我使用register_post_type()函数在WP中创建了一个自定义帖子类型(遵循this article中提供的示例)

它工作正常,但我希望能够在查看列表时过滤管理面板中的帖子。现在我只能按发布日期进行过滤,但我想按作者或任意字段(包括自定义)的值过滤说明。

当我注册自定义帖子类型时,这可以在functions.php中实现吗? 这是我的代码:

// A custom post type for 'film' items
function film_custom_init() {
    $args = array(
      'public' => true,
      'label'  => 'Films',
      'show_ui' => true,
      'show_in_menu' => true,
      'rewrite' => array( 'slug' => 'film' ),
      'support' => array( 'title', 'editor', 'author', 'custom-fields' )
    );
    register_post_type( 'film', $args );
}

add_action( 'init', 'film_custom_init' );

2 个答案:

答案 0 :(得分:1)

在主题的functions.php中添加此代码。希望这会有所帮助

  add_action( 'restrict_manage_posts', 'custom_post_author_wise_filter' );

    function custom_post_author_wise_filter(){
        if (isset($_GET['post_type']) && 'film' == $_GET['post_type']){
            wp_dropdown_users( array(
                'show_option_all' => __( 'Show All Authors', 'twentyfifteen' ),
                'name' => 'author_id',
                'selected' => $_GET['author_id']
            ));
        }
    }
    add_filter( 'parse_query', 'parse_query_custom_post_author_wise_filter' );

    function parse_query_custom_post_author_wise_filter( $query ){
        global $pagenow;
        if ( isset($_GET['post_type']) 
             && 'film' == $_GET['post_type'] 
             && is_admin() && 
             $pagenow == 'edit.php' 
             && isset($_GET['author_id']) 
             && $_GET['author_id'] != '') {
            $query->query_vars['author'] = $_GET['author_id'];
        }
    }

答案 1 :(得分:1)

chaneg与您在高级自定义字段中创建的字段名的关系。 repalce团队发布到你的帖子typein钩子名称和其他位置,并将整个代码放在你的functions.php

add_filter('manage_edit-team_columns', 'custom_posts_table_head');
    function custom_posts_table_head( $columns ) {
        $columns['genre']  = 'Genre';
        return $columns;

    }
    add_action( 'manage_team_posts_custom_column', 'custom_posts_table_content', 10, 2);
    function custom_posts_table_content( $column_name,$post_id ) {
        if( $column_name == 'genre' ) {
            $post = get_field( 'relationship', $post_id );
            echo $post->post_title;
        }
    }

    add_action( 'restrict_manage_posts', 'custom_post_author_wise_filter' );
        function custom_post_author_wise_filter(){
            if (isset($_GET['post_type']) && 'team' == $_GET['post_type']){
                wp_dropdown_posts( array(
                    'show_option_all' => __( 'Show All Genre', 'twentyfifteen' ),
                    'select_name' => 'genre_id',
                    'selected' => $_GET['genre_id']
                ));
            }
        }

        function parse_query_custom_post_author_wise_filter( $query ) {

            if( !is_admin() || $query->get( 'post_type' ) != 'team' )
                return $query;

            if (  isset($_GET['genre_id']) && $_GET['genre_id'] != '') {

            $query->set('meta_query', array(
                array(
                    'key' => 'relationship'
                    ,'value' => $_GET['genre_id']
                    ,'compare' => '='
                ),
            ));
         }
        return $query;
    }
    add_filter('pre_get_posts','parse_query_custom_post_author_wise_filter');

        function wp_dropdown_posts( $args = '' ) {
            $defaults = array(
                'selected'              => FALSE,
                'pagination'            => FALSE,
                'posts_per_page'        => - 1,
                'post_status'           => 'publish',
                'cache_results'         => TRUE,
                'cache_post_meta_cache' => TRUE,
                'echo'                  => 1,
                'select_name'           => 'genre_id',
                'id'                    => '',
                'class'                 => '',
                'show'                  => 'post_title',
                'show_callback'         => NULL,
                'show_option_all'       => 'Select all Genre',
                'show_option_none'      => NULL,
                'option_none_value'     => '',
                'multi'                 => FALSE,
                'value_field'           => 'ID',
                'order'                 => 'ASC',
                'orderby'               => 'post_title',
            );
            $r = wp_parse_args( $args, $defaults );

            $posts  = get_posts( array(
                'post_type'     => 'post',
                'posts_per_page'=> - 1,
                'post_status'   => 'publish',
                    )
                );



            $output = '';
            $show = $r['show'];

            if( ! empty($posts) ) {

                $name = esc_attr( $r['select_name'] );
                if( $r['multi'] && ! $r['id'] ) {
                    $id = '';
                } else {
                    $id = $r['id'] ? " id='" . esc_attr( $r['id'] ) . "'" : " id='$name'";
                }

                $output = "<select name='{$name}'{$id} class='" . esc_attr( $r['class'] ) . "'>\n";
                if( $r['show_option_all'] ) {
                    $output .= "\t<option value='0'>{$r['show_option_all']}</option>\n";
                }
                if( $r['show_option_none'] ) {
                    $_selected = selected( $r['show_option_none'], $r['selected'], FALSE );
                    $output .= "\t<option value='" . esc_attr( $r['option_none_value'] ) . "'$_selected>{$r['show_option_none']}</option>\n";
                }
                foreach( (array) $posts as $post ) {
                    $value   = ! isset($r['value_field']) || ! isset($post->{$r['value_field']}) ? $post->ID : $post->{$r['value_field']};
                    $_selected = selected( $value, $r['selected'], FALSE );
                    $display = ! empty($post->$show) ? $post->$show : sprintf( __( '#%d (no title)' ), $post->ID );
                    if( $r['show_callback'] ) $display = call_user_func( $r['show_callback'], $display, $post->ID );
                    $output .= "\t<option value='{$value}'{$_selected}>" . esc_html( $display ) . "</option>\n";
                }
                $output .= "</select>";
            }
            $html = apply_filters( 'wp_dropdown_posts', $output, $r, $posts );
            if( $r['echo'] ) {
                echo $html;
            }
            return $html;
        }