janlindso
janlindso

Reputation: 1253

Custom permalink for custom post type

I have created a system for a WordPress site using fake pages, and some plugins like More Types (for custom post types), Advanced Custom Fields, and More Taxonomies. The site have a post type for bands and releases.

Everything works very well on frontend, and if a user wanna read about a band, he clicks the menu and ends up at /band/[bandname]. If he wanna read about a release he ends up at /band/[bandname]/releases/[releasename].

As I said, everything works fine in frontend. But when I create a release in backend I'm having a hard time to add the /band/[bandname] part to the permalink for the post type releases. I tried with /band/%band%/releases/, but %band% will be written literally, as it doesn't know where to get the band name from. Any ideas?

I tried to use this code. Band is a post object input in the releases submit form.

add_filter('post_type_link', 'custom_permalinks', 10, 3);

function custom_permalinks($permalink, $post, $leavename)

{

$post_id = get_post_meta($_POST['band'], 'bands', true);

if($post->post_type != 'utgivelse' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
return $permalink;
$var1 = get_post_meta($post_id, 'bands', true);

$var1 = sanitize_title($var1);


$permalink = str_replace('%band%', $var1, $permalink);

return $permalink;

}

Upvotes: 0

Views: 181

Answers (2)

janlindso
janlindso

Reputation: 1253

By doing some small tweaks, I got it working good. I added the following permalink rule to my releasestype: /band/%band%/releases/

Then I created this to replace variable %band% in permalink:

add_filter('post_type_link', 'custom_permalinks', 10, 3);

function custom_permalinks($permalink, $post, $leavename)

{
$post_id = $post->ID;

if($post->post_type != 'utgivelse' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
return $permalink;
$var1 = get_the_title(get_post_meta($post_id, 'band', true));

$var1 = sanitize_title($var1);


$permalink = str_replace('%band%', $var1, $permalink);

return $permalink;

}

Upvotes: 0

Alberto
Alberto

Reputation: 313

You could do it like this:

<?php

add_filter('rewrite_rules_array','customRewriteRules');
add_filter('query_vars','customRewriteVars');

// Remember to flush_rules() when adding rules

add_filter('init','flushRules');

function flushRules(){
  global $wp_rewrite;
    $wp_rewrite->flush_rules();
}


// Adding a new rule
function customRewriteRules($rules)
{
  $newrules = array();
  $newrules['bands-page/([^/]+)/([^/]+)/?'] = 'index.php?pagename=bands-page&customvar1=$matches[1]&customvar2=$matches[2]';
  $newrules['bands-page/([^/]+)/?'] = 'index.php?pagename=bands-page&customvar1=$matches[1]';  // pagename could be band/
  $finalrules = $newrules + $rules;
        return $finalrules;
}

function customRewriteVars($vars)
{
    array_push($vars, 'customvar1', 'customvar2');
    return $vars;
}

You can pass as many as query vars as you want, and then use that var ($_GET['customvar1']) to do a custom loop or something like that.

Upvotes: 1

Related Questions