Using Custom Fields in Custom Post Type URL

I'm developing a car site that has showrooms. Each showroom needs it's own custom URL based on it's locat

I'm developing a car site that has showrooms. Each showroom needs it's own custom URL based on it's location. The location (City and County/State) is already inserted as two custom fields. So for example, if it's "Showroom A", located in Liverpool, Merseyside, it's URL would be the following:-

/

I have a custom post type of "Showroom", that has it's rewrite rules set to false (though it was previously set to true earlier in the test). However when I create the post in question, there's an issue. The permalink is given as everything BAR the name of the showroom (i.e. /). Visting this URL causes a 404 error, and even adding "showroom-a" (for example) to the end of the code (/), also causes a 404 error. Here is my code.

function add_rewrite_rules()
{
    // Register custom rewrite rules

    global $wp_rewrite;

    $wp_rewrite->add_rewrite_tag('%showroom%', '([^/]+)', 'showroom=');
    $wp_rewrite->add_rewrite_tag('%post_custom_data%', '([^/]+)', 'post_custom_data=');
    $wp_rewrite->add_permastruct('showroom', 'location/%post_custom_data%', false);

}

function permalinks($permalink, $post, $leavename)
{
    $no_data = 'no-data';
    $post_id = $post->ID;

    if($post->post_type != 'showroom' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) {
        return $permalink;
    }

    $state = sanitize_title_with_dashes(get_post_meta($post_id, 'state', true));
    $city =  sanitize_title_with_dashes(get_post_meta($post_id, 'city', true));
    $data = $state . "/" . $city;

    if (!$data) {
        $data = $no_data;
    }

    $permalink = str_replace('%post_custom_data%', $data, $permalink);

    return $permalink;    
}

add_action('init', 'add_rewrite_rules');
add_filter('post_type_link', 'permalinks', 10, 3);

Any ideas or help would be appreciated :)

I'm developing a car site that has showrooms. Each showroom needs it's own custom URL based on it's location. The location (City and County/State) is already inserted as two custom fields. So for example, if it's "Showroom A", located in Liverpool, Merseyside, it's URL would be the following:-

http://www.domain/location/merseyside/liverpool/showroom-a/

I have a custom post type of "Showroom", that has it's rewrite rules set to false (though it was previously set to true earlier in the test). However when I create the post in question, there's an issue. The permalink is given as everything BAR the name of the showroom (i.e. http://www.domain/location/merseyside/liverpool/). Visting this URL causes a 404 error, and even adding "showroom-a" (for example) to the end of the code (http://www.domain/location/merseyside/liverpool/showroom-a/), also causes a 404 error. Here is my code.

function add_rewrite_rules()
{
    // Register custom rewrite rules

    global $wp_rewrite;

    $wp_rewrite->add_rewrite_tag('%showroom%', '([^/]+)', 'showroom=');
    $wp_rewrite->add_rewrite_tag('%post_custom_data%', '([^/]+)', 'post_custom_data=');
    $wp_rewrite->add_permastruct('showroom', 'location/%post_custom_data%', false);

}

function permalinks($permalink, $post, $leavename)
{
    $no_data = 'no-data';
    $post_id = $post->ID;

    if($post->post_type != 'showroom' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) {
        return $permalink;
    }

    $state = sanitize_title_with_dashes(get_post_meta($post_id, 'state', true));
    $city =  sanitize_title_with_dashes(get_post_meta($post_id, 'city', true));
    $data = $state . "/" . $city;

    if (!$data) {
        $data = $no_data;
    }

    $permalink = str_replace('%post_custom_data%', $data, $permalink);

    return $permalink;    
}

add_action('init', 'add_rewrite_rules');
add_filter('post_type_link', 'permalinks', 10, 3);

Any ideas or help would be appreciated :)

Share Improve this question edited May 24, 2019 at 19:33 Dan Loewenherz 1054 bronze badges asked Oct 3, 2012 at 15:50 Rhys WynneRhys Wynne 4329 silver badges17 bronze badges 2
  • a couple of things you could try- separate your city and county/state into distinct rewrite tags, and specify the permastruct (with additional tags) as the rewrite argument of your post type registration rather than setting it to false – Milo Commented Oct 3, 2012 at 17:07
  • Cheers @Milo, I've done a bit of work and managed to fine a small issue. Basically, for some reason, $data = $state . "-" . $city works, but $data = $state . "/" . $city does not. Will keep playing... – Rhys Wynne Commented Oct 4, 2012 at 8:40
Add a comment  | 

1 Answer 1

Reset to default 4

I managed to fix this.

Basically I changed the permastruct to this:

$wp_rewrite->add_permastruct('showroom', 'location/%state%/%city%/%showroom%', false);

I then grabbed state & city as two separate variables, replacing it in the permalink structure using these lines:

$permalink = str_replace('%state%', $state, $permalink);
$permalink = str_replace('%city%', $city, $permalink);

With $state & $city grabbed using get_post_meta from the post.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745467143a4628966.html

相关推荐

  • Using Custom Fields in Custom Post Type URL

    I'm developing a car site that has showrooms. Each showroom needs it's own custom URL based on it's locat

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信