We are creating a website in WordPress and we need a unique ID in the permalink structure for a jobs post type. The permalink structure looks like this: http://headway.xpandcreative.co.uk/jobs/5934-sales-executive-4/
With the ID at the front being the post ID. The problem is, if we go to a URL like: http://headway.xpandcreative.co.uk/jobs/5934-sales-executive-4dediejidjiedjiejdiendjeijdejdjie/ it still displays the job even though it should clearly be a 404.
The code I use to create this permalink structure is:
// Rewrite permalink structure
function jobs_rewrite() {
global $wp_rewrite;
$queryarg = 'post_type=jobs&p=';
$wp_rewrite->add_rewrite_tag( '%cpt_id%', '([^/]+)', $queryarg );
$wp_rewrite->add_permastruct( 'jobs', '/jobs/%cpt_id%/', false );
}
add_action( 'init', 'jobs_rewrite' );
function jobs_permalink( $post_link, $id = 0, $leavename ) {
global $wp_rewrite;
// $post = &get_post( $id );
global $post;
if ( is_wp_error( $post ) )
return $post;
$newlink = $wp_rewrite->get_extra_permastruct( 'jobs' );
$newlink = str_replace( '%cpt_id%', $post->ID . '-' . $post->post_name, $newlink );
$newlink = home_url( user_trailingslashit( $newlink ) );
return $newlink;
}
add_filter('post_type_link', 'jobs_permalink', 1, 3);
Please check out the website here http://headway.xpandcreative.co.uk/jobs/
Can anyone give me a suggestion on this?
Thanks
Plugin Debug This may help you figuring out which rewrite rule causes the job page being loaded. I had a similar problem and solved it by deleting and inserting rewrite rules using the rewrite_rules_array filter and additionally manipulating the posts query by means of the posts_where filter. Hope it helps.
You have $queryarg = 'post_type=jobs&p='
which means in your example turns into $queryarg = 'post_type=jobs&p=5934-sales-executive-4'
.
Since p
is intended to be post ID which type is int
, Wordpress sanitizes the input.
I haven't searched how it is done exactly but when you try yourself to do something like:
$returnValue = intval('5934-sales-executive-4', 10);
you'll get in this case value 5934
, and I think that is happening here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.