0

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();
1

THE ANSWER

First of all, this link helped me a lot

https://premium.wpmudev.org/blog/building-customized-urls-wordpress

It turned out the I don't need to use add_rewrite_tag(), but to use add_rewrite_rule() to achieve my goal.

I wondered why --

%job_id% (or even %post_id%, tried them both)

is not recognized in my permalink structure. I was expecting that %job_id% will be replaced by my custom_post's id as it is the tag I used for add_rewrite_tag(), pointing to "p" query var.

And only %postname% is being recognized correctly.

Then, I removed the callback for add_filter('post_type_link')

Result : When I save the new post, the permalink is becoming as is "job-post/%job_id%", %job_id% not being a variable but a hardcoded string.

Then I turned off everything

Result : I discovered that the original query URL turned out to be:

www.mysite.com/?thjb_job=THE_NAME_OF_THE_JOB_POST_ITEM

Which I assume is the reason behind why "p" query var is not recognized, because only the postname is provided.

So I turned everything back on again, and modified the rewriteRules() method

Here is the modification as compared to THE CODES above :

/**
 * 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%';

    // Add the custom permalink structure to wordpess
    add_permastruct('thjb_job', $structure, false);

    // Define the original query url that should be executed to grab the post
    $query = 'index.php?post_type=thjb_job&p=$matches[1]';

    // Apply the query to be called when user bumps into this url pattern
    add_rewrite_rule('^job-post/([^/]+)', $query, 'top');

}

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.