admin管理员组

文章数量:1126457

I am confused around how the add_rewrite_rule function works, especially with custom post types.

I have a custom post type books and for each post, I have a ACF field that is called ISBN. Right now, my permalink structure is:

/books/%post-title%/

But I would like for it to be:

/books/%isbn%/%post-title%/

I had originally attempted this:

function wpd_test_cpt() {

    add_rewrite_tag( '%isbn%', '([0-9]+)' , 'isbn=' );
    add_rewrite_rule(
        'books/([0-9]+)/([^/]+)/?$',
        'index.php?post_type=books&isbn=$matches[1]&name=$matches[2]',
        'top'
    );

}
add_action( 'init', 'wpd_test_cpt' );

 function wpd_test_post_type_link( $permalink, $post ) {

    if ( 'books' === $post->post_type ) {
        if( $isbn = get_field( 'isbn', $post->ID ) ) {
            $permalink = str_replace( ['%isbn%'], [$isbn], $permalink );
        }
    }

    return $permalink;
}
add_filter( 'post_type_link', 'wpd_test_post_type_link', 10, 2 );

And then tried to flush the rewrite rules, but it did not change anything on my site.

How can I grab the ISBN value from the postmeta and add it to the permalink for each of the custom post type posts?

I am confused around how the add_rewrite_rule function works, especially with custom post types.

I have a custom post type books and for each post, I have a ACF field that is called ISBN. Right now, my permalink structure is:

/books/%post-title%/

But I would like for it to be:

/books/%isbn%/%post-title%/

I had originally attempted this:

function wpd_test_cpt() {

    add_rewrite_tag( '%isbn%', '([0-9]+)' , 'isbn=' );
    add_rewrite_rule(
        'books/([0-9]+)/([^/]+)/?$',
        'index.php?post_type=books&isbn=$matches[1]&name=$matches[2]',
        'top'
    );

}
add_action( 'init', 'wpd_test_cpt' );

 function wpd_test_post_type_link( $permalink, $post ) {

    if ( 'books' === $post->post_type ) {
        if( $isbn = get_field( 'isbn', $post->ID ) ) {
            $permalink = str_replace( ['%isbn%'], [$isbn], $permalink );
        }
    }

    return $permalink;
}
add_filter( 'post_type_link', 'wpd_test_post_type_link', 10, 2 );

And then tried to flush the rewrite rules, but it did not change anything on my site.

How can I grab the ISBN value from the postmeta and add it to the permalink for each of the custom post type posts?

Share Improve this question asked Jan 8, 2024 at 1:59 zeropsizeropsi 1014 bronze badges 3
  • I think you need to declare arguments in slug when creating the CPT – mysticalghoul Commented Jan 12, 2024 at 6:19
  • This may be a repeat of this question? wordpress.stackexchange.com/questions/217075/… – rudtek Commented Jan 12, 2024 at 16:21
  • 1 @mysticalghoul was right, so have you already tried doing that? Also, a side note - if you wanted to use the custom field for searching or filtering posts, e.g. to search for posts by a specific ISBN, then you should consider using a custom taxonomy instead, because taxonomy queries are better (than meta queries) for such purposes. – Sally CJ Commented Jan 17, 2024 at 2:47
Add a comment  | 

1 Answer 1

Reset to default 0

Actually, for what you are trying to do, you do not need to manually add a new rewrite tag or rule.

If you want to change the permalink structure of a custom post type, then use the rewrite parameter for register_post_type() and set the rewrite slug to your preferred permalink structure, like so:

$args = array(
    'label'   => 'Books',
    'public'  => true,
    'rewrite' => array(
        // This means that the permalink URL would be
        // https://example.com/books/<ISBN>/<post slug>
        'slug' => 'books/%isbn%',
    ),
    // Your other args.
);

register_post_type( 'books', $args );

Then use the post_type_link hook to replace the %isbn% in the permalink with the correct value.

So looking at your code, you do not need this part: add_action( 'init', 'wpd_test_cpt' );, but you do need this: add_filter( 'post_type_link', 'wpd_test_post_type_link', 10, 2 );.

PS: Don't forget to flush the rewrite rules after removing the unnecessary part above and changing the CPT's rewrite slug.

本文标签: url rewritingIs there a way to add a ACFmetadata field data to a custom post type permalink