Custom Post type permalink structure with custom_post_id

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');

}