Please help: 404 error with WordPress customization

  • Thread starter Thread starter Deleted member 752298
  • Start date Start date
Status
Not open for further replies.
D

Deleted member 752298

Guest
Hi everyone,

I need a little help with the following code. I am using WP Job Manager on a website and the developer of a theme has provided me the following code to customize the slug of WP Job Manager for categories.

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

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

    $replace = array(
        $job_listing_category
    );

    $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%';
  return $args; 
}

The above code is perfectly working. However, I would like to remove the /job/ part from this slug. When I do so, the job listings returns in a 404 error. So when I change this:

function change_job_listing_slug( $args ) {
$args['rewrite']['slug'] = 'job/%category%';
return $args;

to this:

function change_job_listing_slug( $args ) {
$args['rewrite']['slug'] = '%category%';
return $args;

It stops working. Is someone able to help me out to make this piece of code working? :)

Thanks in advance.
 
I am not able to replicate your problem on my test-website, but I think I know where the problem is.
Did you re-save the permalinks after changing making that change in the functions.php? And if the problem is still there, most likely the slug changing function it is getting confused with the posts categories (what if you change that 'job' with something else, like: 'work', will that work?)
 
Try leaving the code as it is and rewrite the query using .htaccess mod_rewrite
 
Maybe... add the slash?

function change_job_listing_slug( $args ) {
$args['rewrite']['slug'] = '/%category%';
return $args;
 
I am not able to replicate your problem on my test-website, but I think I know where the problem is.
Did you re-save the permalinks after changing making that change in the functions.php? And if the problem is still there, most likely the slug changing function it is getting confused with the posts categories (what if you change that 'job' with something else, like: 'work', will that work?)

Try leaving the code as it is and rewrite the query using .htaccess mod_rewrite

Maybe... add the slash?

function change_job_listing_slug( $args ) {
$args['rewrite']['slug'] = '/%category%';
return $args;

Thanks for your input guys! I decided to change the slug using a 3rd party add-on and it's fixed now. :)
 
Status
Not open for further replies.
Back
Top