Need to add rewrite rule that adds in additional information about the post to url

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}

Read More

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?

Related posts

Leave a Reply

1 comment

  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.