在edit.php中对自定义帖子类型进行排序:帖子消失

时间:2012-11-09 作者:kristina childs

我有一些具有不同分类法的自定义帖子类型,我有一个功能,可以在编辑中向帖子列表视图添加某些类别。php管理屏幕。那里一切都很好。我还添加了一个函数,允许用户按这些类别进行排序。这就是问题所在。当你按名称或日期排序时,一切都很好,但当你按自定义类别排序时,所有帖子都会消失,我不知道为什么。

这是通过自定义post类型注册返回的所有代码。所讨论的代码是最后一块,但我认为人们可能需要在上下文中看到整个内容

// Add custom post type Artists
    add_action( \'init\', \'create_post_type\' );
    function create_post_type() {
        register_post_type( \'band_artists\',
            array(
                \'labels\' => array(
                    \'name\' => __( \'Artists\' ),
                    \'singular_name\' => __( \'Artist\' ),
                    \'add_new\' => __( \'Add New\',\'Artist\' ),
                    \'add_new_item\' => __( \'Add New Artist\' ),
                    \'edit_item\' => __( \'Edit Artist\' ),
                    \'new_item\' => __( \'New Artist\' ),
                    \'view_item\' => __( \'View Artist\' ),
                    \'search_items\' => __( \'Search Artists\' ),
                    \'not_found\' => __( \'No Artists Found\' ),
                    \'not_found_in_trash\' => __( \'No Artists In Trash\' ),
                    \'parent_item_colon\' => \'\'
                ),
            \'public\' => true,
            \'has_archive\' => true,
            \'supports\' => array( \'title\', \'editor\', \'comments\', \'excerpt\', \'custom-fields\', \'thumbnail\' ),
            \'rewrite\' => array(\'slug\' => \'artists\'),
            \'taxonomies\' => array(\'category\', \'post_tag\')
            )
        );
        register_taxonomy(\'large_feature\', array(\'band_artists\'), array(\'hierarchical\' => true, \'label\' => \'Large Feature\', \'rewrite\' => false));
        register_taxonomy(\'small_feature\', array(\'band_artists\'), array(\'hierarchical\' => true, \'label\' => \'Small Feature\', \'rewrite\' => false));
    }

// Add custom taxonomies to Admin "Post List" Page
    add_filter( \'manage_edit-band_artists_columns\', \'my_edit_band_artists_columns\' ) ;
    function my_edit_band_artists_columns( $columns ) {

        $columns = array(
            \'cb\' => \'<input type="checkbox" />\',
            \'title\' => __( \'Artists\' ),
            \'large_feature\' => __( \'Large Feature\' ),
            \'small_feature\' => __( \'Small Feature\' ),
            \'date\' => __( \'Date\' )
        );
        return $columns;
    }

// Make those columns display data
    add_action( \'manage_band_artists_posts_custom_column\', \'my_manage_band_artists_columns\', 10, 2 );
    function my_manage_band_artists_columns( $column, $post_id ) {
        global $post;
        switch( $column ) {
            case \'large_feature\' :
                $terms = get_the_terms( $post_id, \'large_feature\' );
                if ( !empty( $terms ) ) {
                    $out = array();
                    foreach ( $terms as $term ) {
                        $out[] = sprintf( \'<a href="%s">%s</a>\',
                            esc_url( add_query_arg( array( \'post_type\' => $post->post_type, \'large_feature\' => $term->slug ), \'edit.php\' ) ),
                            esc_html( sanitize_term_field( \'name\', $term->name, $term->term_id, \'large_feature\', \'display\' ) )
                        );
                    }
                    echo join( \', \', $out ); }
                else { _e( \'\' ); }
            break;
            case \'small_feature\' :
                $terms = get_the_terms( $post_id, \'small_feature\' );
                if ( !empty( $terms ) ) {
                    $out = array();
                    foreach ( $terms as $term ) {
                        $out[] = sprintf( \'<a href="%s">%s</a>\',
                            esc_url( add_query_arg( array( \'post_type\' => $post->post_type, \'small_feature\' => $term->slug ), \'edit.php\' ) ),
                            esc_html( sanitize_term_field( \'name\', $term->name, $term->term_id, \'small_feature\', \'display\' ) )
                        );
                    }
                    echo join( \', \', $out ); }
                else { _e( \'\' ); }
            break;
            default : 
        break; 
        }
    }

// Make those columns sortable
    add_filter( \'manage_edit-band_artists_sortable_columns\', \'my_band_artists_sortable_columns\' );
    function my_band_artists_sortable_columns( $columns ) {
        $columns[\'large_feature\'] = \'large_feature\';
        return $columns;
    }
    add_action( \'load-edit.php\', \'my_edit_band_artists_load\' );
    function my_edit_band_artists_load() {
        add_filter( \'request\', \'my_sort_band_artistss\' );
    }
    function my_sort_band_artistss( $vars ) {
        if ( isset( $vars[\'post_type\'] ) && \'band_artists\' == $vars[\'post_type\'] ) {
            if ( isset( $vars[\'orderby\'] ) && \'large_feature\' == $vars[\'orderby\'] ) {
                $vars = array_merge(
                    $vars,
                    array(
                        \'meta_key\' => \'large_feature\',
                        \'orderby\' => \'meta_value\'
                    )
                );
            }
        }

        return $vars;
    }

1 个回复
最合适的回答,由SO网友:Mridul Aggarwal 整理而成

问题是\'meta_key\' => \'large_feature\', 在上一个函数中,“large\\u特性”是一种分类法,而不是“meta\\u键”。事实上,wordpress中没有一篇文章具有“大\\u特性”作为“meta\\u键”。这就是wordpress没有显示任何帖子的原因。

关于按分类术语排序,wordpress不允许这样。但是,您可以使用直接自定义SQL查询posts_orderby 或此类过滤器,请参阅http://codex.wordpress.org/Class_Reference/WP_Query#Filters

结束

相关推荐

已将wordPress从本地主机移至live,wp-admin显示白屏

嗨,我在本地主机上用wordpress开发了一个网站,然后将其移动到一个实时服务器上。除了wp管理外,一切正常。当我转到域/wp管理时,它会显示一个白色屏幕。然而,当我进入域/wp登录时。php它允许我登录到管理员。此外,如果我尝试创建新帖子或更新任何设置,它会一直显示一个空白的白色屏幕,但当刷新时,会保存设置/添加帖子等。网站是www.nuvogadgets。com。有什么想法吗?谢谢