Unable to have same prepended permalink for jobs and pages
-
Hi,
I have used your instructions to customise my job permalink structure and it is working as intended, I have my job URL’s as follows:/find/category/state/suburb-postcode/job-title/
However, I also have wordpress pages that have the following URL:
/find/category/state/suburb-postcodeThis has resulted in the pages receiving 404 errors.
When I change the permalink structure for jobs to something other than ‘find’ e.g if I change it to:
/job/category/state/suburb-postcode/job-title/Both the pages and the listings work with no 404 error.
Is it possible to have the URL pre-pended with the same start ie ‘find’ for the jobs and wp pages?
The code I’m using to change my permalink is:
/**
* Add rewrite tags for the data we want to insert and define the structure of the permalink.
*
* You need to go to Settings > Permalinks and save for these rules to be added.
*/
function add_job_rewrite_rules(){
add_rewrite_tag( ‘%state%’, ‘([^/]+)’, ‘state=’ );
add_rewrite_tag( ‘%address%’, ‘([^/]+)’, ‘address=’ );
//add_permastruct( ‘job_listing’, ‘find/%state%/%job_listing%’, true );
}
add_action( ‘init’, ‘add_job_rewrite_rules’, 11 );function job_listing_post_type_link( $permalink, $post ) {
// Abort if post is not a job
if ( $post->post_type !== ‘job_listing’ )
return $permalink;// Abort early if the placeholder rewrite tag isn’t in the generated URL
if ( false === strpos( $permalink, ‘%’ ) )
return $permalink;// Get the custom taxonomy terms in use by this post
$categories = wp_get_post_terms( $post->ID, ‘job_listing_category’, array( ‘orderby’ => ‘parent’, ‘order’ => ‘ASC’ ) );
//$regions = get_post_meta(get_the_ID(), ‘geolocation_state_short’, true);
$state = get_post_meta(get_the_ID(), ‘geolocation_state_short’, true);
$suburb = get_post_meta(get_the_ID(), ‘geolocation_city’, true);
$postcode = get_post_meta(get_the_ID(), ‘geolocation_postcode’, true);
$address = $suburb . ‘-‘ . $postcode;if ( empty( $categories ) ) {
// If no terms are assigned to this post, use a string instead (can’t leave the placeholder there)
$job_listing_category = _x( ‘uncategorized’, ‘slug’ );
} else {
// Replace the placeholder rewrite tag with the first term’s slug
$first_term = array_shift( $categories );
$job_listing_category = $first_term->slug;
}// We need data, so fallback to this if its empty
if ( empty( $state ) ) {
$state = ‘statenotthere’;
}if ( empty( $suburb ) ) {
$suburb = ‘suburbnotthere’;
}if ( empty( $postcode ) ) {
$postcode = ‘postcodenotthere’;
}$find = array(
‘%category%’,
‘%state%’,
‘%address%’);
$replace = array(
$job_listing_category,
$state,
$address
);$replace = array_map( ‘sanitize_title’, $replace );
$permalink = str_replace( $find, $replace, $permalink );
return $permalink;
}
add_filter( ‘post_type_link’, ‘job_listing_post_type_link’, 10, 2 );function change_job_listing_slug( $args ) {
$args[‘rewrite’][‘slug’] = ‘find/%category%/%state%/%address%’;return $args;
}
add_filter( ‘register_post_type_job_listing’, ‘change_job_listing_slug’ );Appreciate your help!
Thanks
- The topic ‘Unable to have same prepended permalink for jobs and pages’ is closed to new replies.