Skip to content

Instantly share code, notes, and snippets.

@mrwweb mrwweb/functions.php Secret
Created Feb 12, 2014

Embed
What would you like to do?
Mostly Working Faux CPT Archive Endpoint
<?php
// PROBLEM: Pretty permalink version doesn't get right query: http://diffchecker.com/mmoyu70w
/*
* Support for additional CPT Archive template
* Thank you: http://wordpress.stackexchange.com/a/133698/9844
*/
// add a new query variable
add_filter('init', 'prefix_pt_archive_var');
function prefix_pt_archive_var() {
global $wp;
$wp->add_query_var('map');
}
// add var to wp_query object
function prefix_map_query_var( $vars ){
$vars[] = "map";
return $vars;
}
add_filter( 'query_vars', 'prefix_map_query_var' );
// add a new rewrite rule
add_filter('init', 'prefix_pt_archive_rw_rule');
function prefix_pt_archive_rw_rule() {
// we're adding »map« as a possibility to the slug
// of the custom post type archive we registered above
// no need to match anything, e.g. with »$matches[1]«
// so we're defaulting the map query variable to 1
// this kind of emulates a endpoint
add_rewrite_rule( "projects/map", "?post_type=prefix_projects" . '&map=1', 'top' );
}
// load the »map« template
add_action('template_redirect', 'prefix_map_template_redirect');
function prefix_map_template_redirect() {
// we're loading the template conditionally,
// but only if we're actually at the »map« "endpoint"
if ( 1 == intval(get_query_var('map')) ) {
// you've to create the template you want to use here
include( get_template_directory().'/archivefor-prefix_projects-map.php' );
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.