Home › Forums › General Issues › add acf value to woocommerce permalink
Hello,
i try to add values to woocommerce product permalink.
my code :
//add rewrite rules
function product_add_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag('%product%', '([^/]+)', 'product=');
$wp_rewrite->add_rewrite_tag('%subtitle%', '([^/]+)', 'subtitle=');
$wp_rewrite->add_rewrite_tag('%artiste%', '([^/]+)', 'artiste=');
$wp_rewrite->add_rewrite_tag('%numero_de_page%', '([^/]+)', 'numero_de_page=');
$wp_rewrite->add_permastruct('product', '/catalogue/%artiste%-%product%-%subtitle%-page-%numero_de_page%/', false);
$wp_rewrite->flush_rules();
}
add_action('init', 'product_add_rewrite_rules', 10, 0);
// replace the rwrite tag by the content
function product_permalinks($permalink, $post, $leavename) {
$post_id = $post->ID;
if ($post->post_type == 'product') {
$subtitle = sanitize_title(get_field('subtitle', $post_id));
$artiste = get_field('artiste', $post_id);
$artistesurname = sanitize_title($artiste->name);
$pagenumber = sanitize_title(get_field('numero_de_page', $post_id));
if ($artiste) {
$permalink = str_replace('%artiste%', $artistesurname, $permalink);
} else {
$permalink = str_replace('%artiste%', 0, $permalink);
}
if ($subtitle) {
$permalink = str_replace('%subtitle%', $subtitle, $permalink);
} else {
$permalink = str_replace('%subtitle%', 0, $permalink);
}
if ($pagenumber) {
$permalink = str_replace('%numero_de_page%', $pagenumber, $permalink);
} else {
$permalink = str_replace('%numero_de_page%', 0, $permalink);
}
}
return $permalink;
}
add_filter('post_type_link', 'product_permalinks', 10, 3);
In the backend it’s showing the change :
But in frontend i get a 404. I tried on an other custom post and i get the same.
Anyone had already the same problem ?
Thanks in advance,
Simon
Hello,
i try to add values to woocommerce product permalink.
my code :
//add rewrite rules
function product_add_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag('%product%', '([^/]+)', 'product=');
$wp_rewrite->add_rewrite_tag('%subtitle%', '([^/]+)', 'subtitle=');
$wp_rewrite->add_rewrite_tag('%artiste%', '([^/]+)', 'artiste=');
$wp_rewrite->add_rewrite_tag('%numero_de_page%', '([^/]+)', 'numero_de_page=');
$wp_rewrite->add_permastruct('product', '/catalogue/%artiste%-%product%-%subtitle%-page-%numero_de_page%/', false);
$wp_rewrite->flush_rules();
}
add_action('init', 'product_add_rewrite_rules', 10, 0);
// replace the rwrite tag by the content
function product_permalinks($permalink, $post, $leavename) {
$post_id = $post->ID;
if ($post->post_type == 'product') {
$subtitle = sanitize_title(get_field('subtitle', $post_id));
$artiste = get_field('artiste', $post_id);
$artistesurname = sanitize_title($artiste->name);
$pagenumber = sanitize_title(get_field('numero_de_page', $post_id));
if ($artiste) {
$permalink = str_replace('%artiste%', $artistesurname, $permalink);
} else {
$permalink = str_replace('%artiste%', 0, $permalink);
}
if ($subtitle) {
$permalink = str_replace('%subtitle%', $subtitle, $permalink);
} else {
$permalink = str_replace('%subtitle%', 0, $permalink);
}
if ($pagenumber) {
$permalink = str_replace('%numero_de_page%', $pagenumber, $permalink);
} else {
$permalink = str_replace('%numero_de_page%', 0, $permalink);
}
}
return $permalink;
}
add_filter('post_type_link', 'product_permalinks', 10, 3);
In the backend it’s showing the change :
But in frontend i get a 404. I tried on an other custom post and i get the same.
Anyone had already the same problem ?
Thanks in advance,
Simon
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.