1

A have a post type for venues which have the attributes title, state, and suburb. Currently using the default WordPress permalinks I get the following format:

mysite.com/venue/{title}

However, I would like to have the full url be:

mysite.com/venue/{state}/{suburb}/{title}

This information is something that can be retrieved from the database, with permalinks off the url format for this would be:

mysite.com/?venue=title

I believe it's also possible to pull it by ID some how...

My question is, how do I create a rewrite rule that pulls this information based on post_id or title to create a url of this format?

Is it even possible?

2
  • How is state and suburb saved in the database: as metadata? taxonomy? Aug 20 '12 at 1:11
  • @MannyFleurmond As fields using the plugin <a href="advancedcustomfields.com">ACF</a>. They can easily be stored as post metadata if that would solve this issue? I'm not sure how to do taxonomies but I could look into that and implement that as well.
    – Matt
    Aug 23 '12 at 2:32
1

Sorry I took so long to write this up. Hope it's still useful.

This is based off of a tutorial found here and some general playing around.

<?php

//This function sets up the permastructure
add_action('init', 'setup_permastruct', 40);

function setup_permastruct() {
    //Setup query vars
    add_rewrite_tag('%state%','([^/]+)');
    add_rewrite_tag('%suburb%','([^/]+)');  
    
    //Permastruct for custom post type
    add_permastruct('venue', 'venue/%state%/%suburb%/%postname%', false);   
}

//This one fills in the blanks of your permastructure so that when WP makes a link for a venue, it creates it with all the right info
add_filter('post_type_link', 'create_permalink', 10, 4);

function create_permalink($permalink, $post_id, $leavename, $sample) {
    $post = get_post($post_id);
    $rewritecode = array(
        '%year%',
        '%monthnum%',
        '%day%',
        '%hour%',
        '%minute%',
        '%second%',
        $leavename? '' : '%postname%',
        '%post_id%',
        '%category%',
        '%author%',
        $leavename? '' : '%pagename%',
        '%state%',
        '%suburb%'
    );
    if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
        $unixtime = strtotime($post->post_date);
 
        $category = '';
        if ( strpos($permalink, '%category%') !== false ) {
            $cats = get_the_category($post->ID);
            if ( $cats ) {
                usort($cats, '_usort_terms_by_ID'); // order by ID
                $category = $cats[0]->slug;
                if ( $parent = $cats[0]->parent )
                    $category = get_category_parents($parent, false, '/', true) . $category;
            }
            // show default category in permalinks, without
            // having to assign it explicitly
            if ( empty($category) ) {
                $default_category = get_category( get_option( 'default_category' ) );
                $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
            }
        }
 
        $author = '';
        if ( strpos($permalink, '%author%') !== false ) {
            $authordata = get_userdata($post->post_author);
            $author = $authordata->user_nicename;
        }
 
        $date = explode(" ",date('Y m d H i s', $unixtime));
        
        //Your custom data
        $state = get_post_meta($post_id, 'state_meta', true);
        $suburb = get_post_meta($post_id, 'suburb_meta', true);;
        
        //Enter permalink manipulations here            
        $rewritereplace = array(
            $date[0],
            $date[1],
            $date[2],
            $date[3],
            $date[4],
            $date[5],
            $post->post_name,
            $post->ID,
            $category,
            $author,
            $post->post_name,
            $state,
            $suburb
            //Add custom tag replacements here
        );
        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    }
    return $permalink;      
}

Key thing to notice is in the second function where I have the comment for your custom data. Replace the second argument of the get_post_meta function calls with the names of your respective custom meta data. After you insert the code, go to wp-admin>Settings>Permalinks and click save to refresh your rewrites and voila.

Let me know if you need clarifying on anything.

1
  • Won't this only affect the permalinks shown in WP Admin though? I can't get my actual URLs to show anything othe than 404 errors. Is there anywhere we can talk, over email?
    – Lee
    Jan 18 '18 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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