I was wondering if anybody has a good idea how to solve this cheap.

Assume there are two urls:

  • example.com/portfolio-xyz
  • example.com/portfolios/someidentification

example.com/portfolios/someidentification shall display the content of: example.com/portfolio-xyz but using a different template.

Is there a way to archive this with minimum effort?


The "landing page" (see above URls) in both cases is a WordPress portfolio custom post type page. So to say: no page and no post post type.

additional information

This shall not only work for one specific entry. It shall be generic for a set of portfolio postings.

So you have many portfolios on a website, and shall be able to access them via a differnt url/url-pattern (to the default url) and then have an other template displayed.

  • 1
    You'll need to be more specific than that. What are portfolios and portfolio-xyz. File an edit with this extra info Nov 17, 2014 at 12:17
  • updated @PieterGoosen: does this help you?
    – helle
    Nov 17, 2014 at 12:19
  • What have you tried so far? What research have you completed? Have you read about the Template Hierarchy? codex.wordpress.org/Template_Hierarchy Nov 17, 2014 at 13:37
  • You could also look at both the single_template and template_include filters Nov 17, 2014 at 13:40
  • @helle This is not a forum, so your edits do not only "help User X or Y", but should help anyone (incl. later readers) to understand your question easily. Questions and answers are references.
    – kaiser
    Nov 17, 2014 at 13:52

2 Answers 2


I don't know what your definition of "simple" is, so not sure this qualifies. It's not really a simple task to do this dynamically.

First, we register a new query var to pass the alternate identifier:

function wpd_portfolios_query_var($query_vars){
    $query_vars[] = 'portfolio_key';
    return $query_vars;
add_filter('query_vars', 'wpd_portfolios_query_var');

Next, we add a rewrite rule to handle incoming requests, and set the query var accordingly:

function wpd_portfolios_rewrite(){
add_action( 'init', 'wpd_portfolios_rewrite' );

Then we hook parse_query, check if our query var is set, and convert these requests to singular post requests. Otherwise WordPress will default to setting these requests to is_home.

function wpd_portfolios_parse( $query ){
    if( isset( $query->query_vars['portfolio_key'] ) ){
        $query->is_home = 0;
        $query->is_single = 1;
        $query->is_singular = 1;    
add_action( 'parse_query', 'wpd_portfolios_parse' );

Next we hook pre_get_posts to set a meta query for the portfolio post with the desired meta value.

function wpd_portfolios_query( $query ){
    if( isset( $query->query_vars['portfolio_key'] ) ){
        $query->set( 'posts_per_page', 1 );
        $meta_query = array(
                'key' => 'portfolio_key',
                'value' => $query->query_vars['portfolio_key']
        $query->set( 'meta_query', $meta_query );
add_action( 'pre_get_posts', 'wpd_portfolios_query' );

And lastly, we filter single_template to load the special template for these requests:

function wpd_portfolios_template( $template = '' ){
    global $wp_query;
    if( isset( $wp_query->query_vars['portfolio_key'] ) ){
        $template = locate_template( 'portfolio-special.php', false );
    return $template;
add_filter( 'single_template', 'wpd_portfolios_template' );

The "cheapest" way to achieve this probably would be to simply add two pages:

// Standard template

Then add another page with your second URl and use a custom page template where you query your single post using get_post() and display it however it pleases you:

/* Template Name: Alternate Portfolio PT template */


// Fetch post via ID
$post = get_post( 123 );
// Set everything up so the API is ready to use
$is_set_up = setup_postdata( $post );
if ( $is_set_up )

// or use a custom query
$post = new WP_Query( array( /* your arguments here */ ) );
if ( have_posts() )
    while ( have_posts )
        // etc.

  • so this is not very generic, but I like the idea. actually it shell work for not one specific posting.
    – helle
    Nov 17, 2014 at 15:10
  • Servus @helle "shell" is the "outer shell" and "shall" comes from "should" (fixed that in your question already) ;) - Please update your question to reflect the fact that you need a "generic and cheap" solution.
    – kaiser
    Nov 17, 2014 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.