1

Im trying to make a small plugin that can produce a team with a trainer. The team is a Custom Post Type and the trainer is a Custom Post Type. When I create a new team, i read all the trainers in a combo-box, where the title of the custom post type traener is the name of the trainer.

My problem is that when I type in a name for the team in the title box, the slug is created with the last title from the traener Custom Post Type title.

This is what i have come up with so far when i create the register the Custom Post Type and create the metabox with the trainer and som other infos.

global $post;
$title = $post->post_title;
echo "<script>alert ('".$title.")</script>";
$args = array(
   'post_type' => 'traener',
   'posts_per_page' => -1,
   'orderby' => 'post_title', // meta_value',
   'order'   => 'ASC',
);
$my_query = new WP_Query($args);
  if ($my_query->have_posts()) {
    while ($my_query->have_posts()) : $my_query->the_post();
      if ($selectedtrainer == get_the_title()) {
        echo '<option value="'.esc_html( get_the_title() ).'"selected=selected>'.esc_html( get_the_title() ).'</option>';                           
      } else {
        echo '<option value="'.esc_html( get_the_title() ).'">'.esc_html( get_the_title() ).'</option>'; 
      } 
    endwhile; 
  }
wp_reset_postdata();

$post->post_title = $title;

Is there any way I can reset the post title from being a post title of the traener custom post type, so the slug will be based on the team title

I have tried using the save_post action to manipulate the title with no luck, and it is also not eksactly what i want, because when you use the tabulator the slug is generated in the background (ajax/javascript).

Really hope someone can help me in the right direction

  • You are using traener instead of trainer in your arguments. Could that be a problem? By the way, do not use wp_reset_query();. Only wp_reset_postdata(); is enough. – Jack Johansson Jul 27 '17 at 4:45
  • Jack Johansson - I have edited the description. Unfortunately it does not help only using wp_reset_postdata();. Thank you for pointing it out. – Jacob Thygesen Jul 27 '17 at 14:15
1

I found that i just have to use

$post->post_name 

instead of

$post->post_title 

because $post->post_name holds the slug also the autogenerated if you save as draft without defining a title.

So the code would as follows

global $post;
$slug = $post->post_name;
echo "<script>alert ('".$title.")</script>";
$args = array(
   'post_type' => 'traener',
   'posts_per_page' => -1,
   'orderby' => 'post_title', // meta_value',
   'order'   => 'ASC',
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
    while ($my_query->have_posts()) : $my_query->the_post();
        if ($selectedtrainer == get_the_title()) {
           echo '<option value="'.esc_html( get_the_title() ).'"selected=selected>'.esc_html( get_the_title() ).'</option>';                           
        } else {
            echo '<option value="'.esc_html( get_the_title() ).'">'.esc_html( get_the_title() ).'</option>'; 
        } 
    endwhile; 
}
wp_reset_postdata();

$post->post_name = $slug;

Hope this can be of some use for others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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