我可以使用元值而不是类别创建自定义帖子类型存档吗?

时间:2018-03-05 16:26:58

标签: php wordpress custom-post-type archive

与通常使用类别的Wordpress博客档案不同,我有一个自定义帖子类型,它使用全局元键和值将这些帖子链接到我网站中的页面。

我想创建一个存档,就像博客上的类别存档一样,但是要处理这些元数据。

使用自定义WP查询并保持分页的最佳方法是什么?同时还要实现正确的归档网址?

function bv_patient_stories_init() {
        register_post_type('patient-story', array(
            'labels'          => array(
                'name'               => 'Patient Stories',
                'singular_name'      => 'Patient story',
                'add_new'            => 'Add new',
                'add_new_item'       => 'Add a new Patient Story',
                'edit_item'          => 'Edit Patient Story',
                'new_item'           => 'New Patient Stories',
                'all_items'          => 'All Patient Stories',
                'view_item'          => 'View Patient Story',
                'search_items'       => 'Search Patient Stories',
                'not_found'          => 'No Patient Stories have been added',
                'not_found_in_trash' => 'No Patient Stories in bin',
                'parent_item_colon'  => '',
                'menu_name'          => 'Patient Stories'
            ),
            'description'     => 'Patient case study',
            'public'          => true,
            'show_ui'         => true,
            'show_in_menu'    => true,
            'query_var'       => true,
            'rewrite'         => array('slug' => 'patient-stories', 'with_front' => false),
            'capability_type' => 'post',
            'has_archive'     => true,
            'hierarchical'    => false,
            'menu_position'   => 57,
            'supports'        => array('title', 'revisions', 'thumbnail', 'excerpt', 'custom-fields', 'comments'),
            'menu_icon'       => 'dashicons-id',
        ));
    }

1 个答案:

答案 0 :(得分:0)

function fwp_home_custom_query( $query ) {
   //check if you are working on archive query
   if ( $query->is_post_type_archive($post_type) ) {

    //the key and value pairs you want
    $meta_query = array(
             array(
                'key'=>'$your_key',
                'value'=>'$value',
                'compare'=>'=',
             ),
    );

    //updating the query
    $query->set( 'orderby', $meta_key);
    $query->set( 'order', 'ASC' );
    $query->set( 'meta_query',$meta_query );

   }
}

add_filter( 'pre_get_posts', 'fwp_home_custom_query

对于url结构,您还需要应用过滤器。