0

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 post permalink 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 Post title = Post permalink

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.

5
  • Instead of having to use the metadata, why don't you just edit the post slug manually via the slug/permalink editor on the post editing screen? You also got an unused variable - $post_type - and note that get_post_type() accepts just one parameter.
    – Sally CJ
    May 10, 2021 at 4:06
  • yes i'm aware of the permalink editor but in long term i"m going to have a lot of posts and i don't want to do this one by one , having everything adjusted is better than using the manual permalink editor
    – Spix
    May 10, 2021 at 15:35
  • only the first meta field is used - what exactly are the other meta you wanted to use? Do you want to use them all in the post slug? Can you edit your question and then show a sample slug having all the meta? Like if the meta are "model" and "year" with the values "Brio" and "2011" respectively, then would the slug be brio-2011 or something else?
    – Sally CJ
    May 12, 2021 at 5:52
  • yes exactly like that "brio-2011" , i added an edit to my question hopefully i didnt make any mistakes , check it out
    – Spix
    May 12, 2021 at 12:34
  • To be honest, I don't fully understand what you said in your question edits.. but anyway, I've posted an answer, so please use the first snippet in the question and apply the changes/suggestions stated in my answer (you can ignore the second and third Additional Notes for now). Let me know.
    – Sally CJ
    May 12, 2021 at 17:35

1 Answer 1

0

only the first meta field is used

Yes, and it's because the $new_slug contains just one meta which is maker.

So if you want to add other meta like model and year, and have a slug that looks like <maker>-<model>-<year>, you can try my code below — just replace the $new_slug = get_post_meta($post_id,'maker', true); in your code with the following:

Note: You should use the first snippet in the question.

// List of meta that will be added to the post slug (post_name). Just add another
// get_post_meta() if you want to add another meta to the slug. And just reposition
// the array items based on your preferences, e.g. you could put the 'year' before
// 'maker'.
$meta = array(
    get_post_meta( $post_id, 'maker', true ),
    get_post_meta( $post_id, 'model', true ),
    get_post_meta( $post_id, 'year', true ),
);

$new_slug = implode( '-', array_filter( $meta ) );

target only my CPT "cars"

You could use get_post_type() like so:

add_action( 'save_post', 'set_slug' );
function set_slug( $post_id ) {
    // Do nothing if the post type is NOT "cars".
    if ( 'cars' !== get_post_type( $post_id ) ) {
        return;
    }

    // ... your code here.
}

But a simpler option is using the save_post_<post type> hook instead of save_post like so:

add_action( 'save_post_cars', 'set_slug' );
function set_slug( $post_id ) {
    // ... your code here.
}

Additional Notes

  1. Just remove the $post_type = get_post_type($post,'cars', true); from your code.

  2. Before you update the post, you should ensure that the slug is not already the same as the value of the $new_slug variable. So for example, add this after the above $new_slug = implode( '-', array_filter( $meta ) );:

    if ( strtolower( $new_slug ) === get_post_field( 'post_name', $post_id ) ) {
        return;
    }
    
  3. "set_slug" is a very generic name, so please use a better name that is unique enough, e.g. wpse_387941_set_slug.. ( and be sure to update the function name in the remove_action() and add_action() calls ).

4
  • Thank you for your detailed answer but sadly its not working , here is the code i used according to your answer , correct me if im wrong of course: link the post its not getting updated with the code above , here a screenshot link
    – Spix
    May 14, 2021 at 16:15
  • here is the final code after checking pretty much everything i could think of and still not working link
    – Spix
    May 14, 2021 at 16:34
  • Well, in your final code, you didn't call wp_update_post().. but anyway, try this and make sure the post type is correct. And you really should use a child theme and put the code in the theme functions.php file, rather than using a plugin to add that code.. but for now, just try my code and let me know.
    – Sally CJ
    May 14, 2021 at 20:03
  • 1
    thank you so much, now its works exactly like i want it
    – Spix
    May 14, 2021 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.