Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. // REGISTER POST TYPE courses
  2. // Nothing special happens here
  3. // REGISTER POST TYPE lessons
  4. // Lessons will have rewrite argument as the following:
  5. 'rewrite' =>
  6. 'slug' => 'school/%course%',
  7. 'with_front' => false
  8. )
  9.  
  10. We need to figure out what we need to replace /%course% with first, this will be the course name that you assigned the lesson to.
  11. So we loop through all the fields from the field where the lessons are assigned. (The code is still dirty atm.)
  12. function updateLessonMeta($post_id){
  13. if(get_post_type() == 'course'){
  14. $fields = $_POST['acf']['field_5c952bc9f09ec'];
  15. foreach($fields as $field){
  16. foreach($field['field_5c952bd5f09ee'] as $key => $value){
  17. // Here we update the field of the lesson with the id of the course we just saved.
  18. update_field('course_id', $post_id, $value['field_5c952bd9f09ef']);
  19. }
  20. }
  21. }
  22. }
  23. add_action('acf/save_post', 'updateLessonMeta', 1);
  24.  
  25. // Now we can use the value of the field we just updated with the function above to get the title of the course and replace %course% with the actual course name
  26. function lessonPostLink($post_link, $id = 0 ){
  27. $post = get_post($id);
  28. echo get_the_id();
  29. if (is_object( $post)){
  30. $course = get_field('course_id');
  31. if( $course ){
  32. return str_replace('%course%', sanitize_title(get_the_title($course)), $post_link );
  33. }
  34. }
  35. return $post_link;
  36. }
  37. add_filter( 'post_type_link', 'lessonPostLink', 1, 1 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement