i have created a custom post type using Jetengine called "cars" and meta field like this "model" "maker" "year" .. etc, what i want is to use more than one meta field as post permalink and target only my CPT "cars" (this one im not sure about it), for now my code looks like this:
add_action('save_post', 'set_slug');
function set_slug($post_id){
$new_slug = get_post_meta($post_id,'maker', true);
$post_type = get_post_type($post,'cars', true);
$post_args = array(
'ID' => $post_id,
'post_name' => $new_slug,
);
// unhook this function to prevent infinite loop
remove_action( 'save_post', 'set_slug' );
wp_update_post($post_args);
// re-hook this function
add_action( 'save_post', 'set_slug' );
}
i tried to add more meta field next to each other but its didn't work , only the first meta field is used
any help is appreciate since i'm still new to this.
thanks
Edit:
im trying to have more meta field into my permalink of CPT posts like this: "www.mywebsite.com/cars/maker-model-year"
at the moment i have my code like below and its working like i want it
but i wish to know how i can add more meta field and make it conditional to a specific CPT
add_action('save_post', 'set_slug');
if (get_post_type($post_object->ID) == 'car') {
function set_slug($post_id){
$new_slug = get_post_meta($post_id, true);
$post_args = array(
'ID' => $post_id,
'post_name' => $new_slug,
);
// unhook this function to prevent infinite loop
remove_action( 'save_post', 'set_slug' );
}
wp_update_post($post_args);
// re-hook this function
add_action( 'save_post', 'set_slug' );
}
I should also add this : i have another code in place that set post title from my form field like this
Form 1 :
Maker + Model + Year = BMW-M3-2011
and this one only works if i set $new_slug like this in my code above $new_slug = get_post_meta($post_id, true);
i apologize if i'm making this confusing , i don't mean to.
$post_type
- and note thatget_post_type()
accepts just one parameter.brio-2011
or something else?