THE QUESTION
Hello!
Currently, my permalink settings are set to "Post Name" so that the default wordpress posts which are used for articles will have a pretty urls.
But then, I have this job post (custom post type as "thjb_job") that needs to have these kinds for permalink structures:
For viewing a single job post
1.) (rootURI) / job-post / %job_id%
and for listing based on employers (employer as a kind of taxonomy)
2.) (rootURI) / %taxonomy% / job-post / %job_id%
My priority is the item no. 1 as I haven't added the employer taxonomy yet.
I have already used wordpress methods like add_rewrite_tag() and add_permastruct() but they seems to not work properly.
I have also tried using add_filter('post_type_link') wordpress hook but it seems to just manipulate the permalink string record as it returns 404 when I tried to view the post (by clicking on the permalink while editing the custom post thjb_job).
Also, I have read a lot of answers that involves modification of .htaccess. I'm hoping that I won't need to touch .htaccess just to achieve this custom permalink structure.
Thanks in advance for your help on how to achieve the permalink structures above.
THE CODES
Here are the ways I'm using the methods mentioned above
/**
* AddCustomPostJob = Add Custom Post labeled as Job
* : this class registers a new custom post type called thjb_job (Job)
*/
class AddCustomPostJob {
/**
* init() = Initializer Method
* : this method is the class engine starter
*
*/
public static function init() {
// Register the custom post type
add_action( 'init', array(get_called_class(), 'registerPostType') );
// Register custom rewrite rules for this custom post type
add_action( 'init', array(get_called_class(), 'rewriteRules') );
// Register custom url tag translations
add_filter( 'post_type_link', array(get_called_class(), 'translateTags'), 10, 2 );
}
/**
* registerPostType = Register Post Type callback
* : this method defines the properties for the custom post type
*/
public static function registerPostType() {
// Define custom type child options
// Labels
$labels = array(
'name' => __( 'Jobs' ),
'singular_name' => __( 'Job' )
);
// Default fields
$supports = array( 'title', 'editor' );
// Turn off wordpress permalink rewrite
$rewrite = false;
// Define the options for the custom post type
$opts = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'show_in_admin_bar' => true,
'supports' => $supports,
'rewrite' => $rewrite,
);
// Register the post type with key => thjb_job
register_post_type( 'thjb_job', $opts);
} // registerPostType()
/**
* rewriteRules() = Custom rewrite rules method
* : this method creates a custom rewrite rules exclusively for thjb_job post type
*
*/
public static function rewriteRules() {
// Define the custom permalink structure
$structure = '/job-post/%job_id%';
// Map the equivalent query var
add_rewrite_tag("%job_id%", '([^/]+)', "p=");
// Add the custom permalink structure to wordpess
add_permastruct('thjb_job', $structure, false);
}
/**
* translateTags() = Translate url tags method
* : this method translates tags (%tags%) to its original value
*
* @param $url
* : this param is the current url to be modified
*
* @param $post
* : this param is the current post being processed
*
* @return modified $url
*
*/
public static function translateTags($url, $post) {
// Apply only for thjb_job post type
if ( $post->post_type == 'thjb_job') {
// If %job_id% exists, then insert the original value
if ( strpos($url, '%job_id%') ) {
return str_replace('%job_id%', $post->ID, $url);
}
}
return $url;
}
} // AddCustomPostJob class
AddCustomPostJob::init();