I am running WordPress 4.0 and I have created a custom post type called events-to-see which is a post. I wanted to get the events to display with the following url structure
/events/[year]/[month]/[title]
I have this part working, thanks to some articles here, but I am trying to select specific years or year/months and get paging working properly. For example:
/events/[year]/[month]/page/2
/events/2015/02/page/2
AND
/events/[year]/page/2
/events/2015/page/2
I am including the code I have below and hoping someone can help me. The /events/ page is an actual page with a page template to display events from selected destinations based on a custom taxonomy.
When I hit the /events/2015/page/2 or /events/2015/02/page/2 I get a listing which looks like the standard archive page with everything including other post types.
Any help you can provide would be greatly appreciated.
thanks, Anthony
// Events Custom Post Type
$labels = array(
'name' => 'Events',
'singular_name' => 'Event',
'menu_name' => 'Events',
'name_admin_bar' => 'Event',
'add_new' => 'Add New',
'add_new_item' => 'Add New Event',
'new_item' => 'New Event',
'edit_item' => 'Edit Event',
'view_item' => 'View Event',
'all_items' => 'All Events',
'search_items' => 'Search Events',
'parent_item_colon' => 'Parent Events:',
'not_found' => 'No Events found.',
'not_found_in_trash' => 'No Events found in trash.'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicaly_queryable' => true,
'taxonomy' => array('post_tag','event-years'),
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-calendar-alt',
'query_var' => true,
'rewrite' => array( 'slug' => 'events/%events_year%/%events_month%', 'with_front' => false ),
/*'rewrite' => array( 'slug' => 'events', 'with_front' => false), */
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'supports' => array('title','editor', 'thumbnail', 'custom-fields')
);
register_post_type (
'events-to-see',
$args
);
//Rewrite Functions
function wpd_event_rewrite_tags() {
add_rewrite_tag( '%events_year%', '([0-9]{4})' );
add_rewrite_tag( '%events_month%', '([0-9]{2})' );
}
add_action( 'init', 'wpd_event_rewrite_tags' );
function wpd_event_permalink($permalink, $post, $leavename) {
if ( get_post_type( $post ) === "events-to-see" ) {
$sd = get_post_meta( $post->ID, 'event_date', true);
$year = date('Y', $sd + get_option( 'gmt_offset' ) * 3600);
$year = substr($sd, 0, 4);
$month = date('m', $sd + get_option( 'gmt_offset' ) * 3600);
$month = substr($sd, 4, 2);
//echo($month);
$rewritecode = array(
'%events_year%',
'%events_month%',
$leavename ? '' : '%postname%',
);
$rewritereplace = array(
$year,
$month,
$post->post_name
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}
add_filter( 'post_type_link', 'wpd_event_permalink', 10, 4 );
function wpd_single_event_queries( $query ){
if( $query->is_singular()
&& $query->is_main_query()
&& isset( $query->query_vars['events'] ) ){
$meta_query = array(
array(
'key' => 'events_year',
'value' => $query->query_vars['events_year'],
'compare' => '=',
'type' => 'numeric',
),
array(
'key' => 'events_month',
'value' => $query->query_vars['events_month'],
'compare' => '=',
'type' => 'numeric',
),
);
$query->set( 'meta_query', $meta_query );
printf($query);
}
}
add_action( 'pre_get_posts', 'wpd_single_event_queries' );
Aucun commentaire:
Enregistrer un commentaire