• Resolved kuboris

    (@kuboris)


    Hi I’m trying to follow this tutorial

    And my goal is to create permalinks – website.com/jobs/location/id/name
    Tutorial in documentation is pretty well written but with my limited knowledge of PHP I haven’t been able to make it work.

    I used this part of code to add jobs is to the base url. But I struggle with adding location.

    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;
    
    $find = array(
    '%post_id%'
    );
    
    $replace = array(
    $post->ID
    );
    
    $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/%post_id%';
    return $args;
    }
    add_filter( 'register_post_type_job_listing', 'change_job_listing_slug' );

    How would you create website.com/job/location/id/job-title structure ?
    All help is appreciated.

    https://www.ads-software.com/plugins/wp-job-manager/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mike Jolley

    (@mikejolley)

    I’m not sure you’ll be able to get that exact structure because meta data cannot be part of the structure (i.e separated by slashes). Meta data includes things like location, company name etc

    You could get something like /job/id/title-location for example though (by default it places the location next to the job title if you submit the job from the frontend).

    Thread Starter kuboris

    (@kuboris)

    Is it possible to not use metadata and create this permalink structure using WP Job Manager – Predefined Regions plugin ?

    Plugin Author Mike Jolley

    (@mikejolley)

    Possibly, if the jobs have terms for the location yes. It is a little more complex to set that up and you would need to ensure each job has a location set. It may require some dev customisation to get it working flawlessly.

    Hello,

    I need some help here… How can I achieve website.com/job/region/category/postname?

    I’ve followed instructions (https://wpjobmanager.com/document/tutorial-changing-the-job-slugpermalink) and placed this on my theme’s functions file:

    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/%region%/%category%';
      return $args;
    }
    add_filter( 'register_post_type_job_listing', 'change_job_listing_slug' );
    
    function add_category_endpoint_tag() {
    	add_rewrite_tag( '%category%', '([^/]*)' );
    }
    add_action( 'init', 'add_category_endpoint_tag' );

    All works perfect but when I click a link to visit any wp job post it gives me 404 error.

    Hope you can help Mike

    Greetings

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Permalink using Location’ is closed to new replies.