0

I'm building a site that uses WP Job Manager. I was interested in setting it up so that URLs show as:

/category/region/name/

I found this link that explains how to do just that from their site and added this code:

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    = wp_get_post_terms( $post->ID, 'job_listing_region', array( 'orderby' => 'parent', 'order' => 'ASC' ) );

    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;
    }

    if ( empty( $regions ) ) {
        // If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
        $job_listing_region = _x( 'anywhere', 'slug' );
    } else {
        // Replace the placeholder rewrite tag with the first term's slug
        $first_term = array_shift( $regions );
        $job_listing_region = $first_term->slug;
    }

    $find = array(
        '%category%',
        '%region%'
    );

    $replace = array(
        $job_listing_category,
        $job_listing_region
    );

    $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'] = 'job/%category%/%region%';
  return $args;
}
add_filter( 'register_post_type_job_listing', 'change_job_listing_slug' );

function add_region_endpoint_tag() {
    add_rewrite_tag( '%region%', '([^/]*)' );
}
add_action( 'init', 'add_region_endpoint_tag' );

Which does exactly what I need it too, URLs show up as /category/region/name/ - However my gallery images and my blog posts all show up as 404.

Even with this included:

// Abort if post is not a job
if ( $post->post_type !== 'job_listing' ) {
    return $permalink;
}

Can anyone please help me, how do I tell WP Job Manager to omit blog posts, and I believe it's attachment post type, when modifying permalinks?

4
  • Other post types usually break before you've flushed permalinks. Have you visited your permalinks page since doing this?
    – VesterDe
    Apr 4, 2016 at 13:40
  • @VesterDe Yep of course :D Apr 4, 2016 at 13:49
  • You could install the Query Monitor plugin, and on the front end side then look at how the request is being interpreted. It will show you what the URL is translated to in terms of post_types and taxonomies. That info could provide some insight.
    – VesterDe
    Apr 4, 2016 at 13:53
  • @VesterDe Will try this to see if anything useful pops up, thanks for the advice! Apr 4, 2016 at 15:16

1 Answer 1

0

I had the same issue on my website http://loctim.com because I wanted the slug like this %category%/%state%/%city% I had 404 pages in changing functions.php and today it is the gallery which was crazy. So i worked all the night and I have found that I had forgotten this part of code :

function add_category_endpoint_tag() {
    add_rewrite_tag( '%category%', '([^/]*)' );
    }
add_action( 'init', 'add_category_endpoint_tag' );

Please try to put that previous code just before (yours) this one :

function add_region_endpoint_tag() {
    add_rewrite_tag( '%region%', '([^/]*)' );
        }
add_action( 'init', 'add_region_endpoint_tag' );

I hope that will help you. I have more code about that slug if you are interested in, for example how to automatically replace %state% by the %country% if the %state% = %city% (some cities are in that case as in usa or Portugal)

3
  • It would be nice if you can explain a bit that what is this code doing exactly!
    – Sumit
    Apr 24, 2016 at 5:52
  • @Charly You are awesome! I just tried your code and it is working, so far your code plus WP Job Manager's code is working as long as I have /some-thing/category/region, therefore that some-thing part for now is the letter 'i' just so that it has something there and URLs load correctly. Thanks so much Charly. Will continue to see it all works and mark your answer correct. If you happen to know how to remove /some-thing/ let me know, otherwise you've helped me a great deal already, thanks!! Apr 25, 2016 at 20:37
  • @Charly could you please explain what the code does? Apr 25, 2016 at 21:04

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.