0
votes

Unlike the usual Wordpress blog archives which work from categories, I have a custom post type which uses a global meta key and values to link these posts with pages in my site.

I want to create an archive, exactly like the category archives on the blog but to work with these meta values.

What's the best way to do this, using the custom WP query, and keeping pagination, but whilst also achieving the correct archive URLs?

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
So you want to create pages with that have a certain meta key value? Such ass get all pages with key that have the value of X?Orlando P.
@OrlandoP. No, that's not what I want to do.Lee
Isn't that is basically what an archive is? A page with posts on it of a certain category. In this case you want those post on the page to only be of certain meta key value pairs.Orlando P.

1 Answers

0
votes
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

as for the url structure you will need to apply a filter as well.