By relm


2012-05-07 23:44:24 8 Comments

How can I add extra parameters after a permalink, specifically if I'm using a custom post type?

For example, let's say http://mysite/album/record-name was the permalink. How can I make http://mysite/album/record-name/related not turn up a 404 or redirect?

WordPress doesn't seem to call up the post template if the post doesn't exist... so I'm at a bit of a loss how to do this.

3 comments

@fuxia 2012-05-08 00:24:23

You can add an endpoint to your URIs to handle special requests.

Here is a basic example as plugin. To understand what's going on read Christopher Davis's fantastic tutorial A (Mostly) Complete Guide to the WordPress Rewrite API.

<?php # -*- coding: utf-8 -*-
/**
 * Plugin Name: T5 Endpoint Example
 * Description: Adds a permalink endpoint to posts named <code>epex</code>
 */

add_action( 'init', 't5_add_epex' );

function t5_add_epex()
{
    add_rewrite_endpoint( 'epex', EP_PERMALINK );
}

add_action( 'template_redirect', 't5_render_epex' );

/**
 * Handle calls to the endpoint.
 */
function t5_render_epex()
{
    if ( ! is_singular() or ! get_query_var( 'epex' ) )
    {
        return;
    }

    // You will probably do something more productive.
    $post = get_queried_object();
    print '<pre>' . htmlspecialchars( print_r( $post, TRUE ) ) . '</pre>';
    exit;
}


add_filter( 'request', 't5_set_epex_var' );

/**
 * Make sure that 'get_query_var( 'epex' )' will not return just an empty string if it is set.
 *
 * @param  array $vars
 * @return array
 */
function t5_set_epex_var( $vars )
{
    isset( $vars['epex'] ) and $vars['epex'] = true;
    return $vars;
}

@T.Todua 2014-04-01 13:59:49

to add parameter to post url (permalink), i use like this:

add_filter( 'post_type_link', 'append_query_string', 10, 2 );
function append_query_string( $url, $post ) 
{
    return $url.'?my_pid='.$post->ID;
}

output:

http://yoursite.com/pagename?my_pid=12345678

@Milo 2012-05-08 00:23:25

You can do this with the Rewrite API's add_rewrite_endpoint:

add_action( 'init', 'wpse51444_endpoint' );
function wpse51444_endpoint(){
    add_rewrite_endpoint( 'related', EP_ALL );
}

add_filter( 'query_vars', 'wpse51444_query_vars' );
function wpse51444_query_vars( $query_vars ){
    // add related to the array of recognized query vars
    $query_vars[] = 'related';
    return $query_vars;
}

In the template you can detect when your related query var is present:

if( array_key_exists( 'related' , $wp_query->query_vars ) ):
    // current request ends in related
endif;

@Hexodus 2013-11-15 14:14:57

What does wpse51444 mean? Is this just a longish string to make sure to not collide with something?

@Milo 2013-11-15 14:39:11

@Hexodus yes, wpse = wp stackexchange, 51444 is the id of this question. You can change that to anything you'd like, but it's good to use something you know will be unique.

@Hexodus 2013-11-15 15:05:04

Oh Thank you Milo for clearification - this was very mysterious ;)

@Nabil Kadimi 2014-11-21 23:57:37

@Hexodus I didn't find it mysterious at all.

@Charlie Vieillard 2016-10-26 08:54:32

Don't forget to flush the permalinks.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Output the slug and name of a CPT single post taxonomy term

1 Answered Questions

[SOLVED] Removing Custom Permalink Structure for Custom Post Type

2 Answered Questions

Permalink for Custom Post Types

1 Answered Questions

[SOLVED] Adding an extra parameter [string] to my posts' permalink?

2 Answered Questions

[SOLVED] Get post title of faulty link on 404 error page

1 Answered Questions

2 Answered Questions

[SOLVED] Unusual Custom Post 404 problem (NOT a permalink issue)

  • 2011-10-24 10:51:24
  • Samuel Hulick
  • 357 View
  • 1 Score
  • 2 Answer
  • Tags:   custom-post-types

1 Answered Questions

[SOLVED] Page Name and Permalinks

  • 2011-09-03 15:29:39
  • tonyf
  • 330 View
  • 0 Score
  • 1 Answer
  • Tags:   permalinks

Sponsored Content