Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // REGISTER POST TYPE courses
- // Nothing special happens here
- // REGISTER POST TYPE lessons
- // Lessons will have rewrite argument as the following:
- 'rewrite' =>
- 'slug' => 'school/%course%',
- 'with_front' => false
- )
- 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.
- So we loop through all the fields from the field where the lessons are assigned. (The code is still dirty atm.)
- function updateLessonMeta($post_id){
- if(get_post_type() == 'course'){
- $fields = $_POST['acf']['field_5c952bc9f09ec'];
- foreach($fields as $field){
- foreach($field['field_5c952bd5f09ee'] as $key => $value){
- // Here we update the field of the lesson with the id of the course we just saved.
- update_field('course_id', $post_id, $value['field_5c952bd9f09ef']);
- }
- }
- }
- }
- add_action('acf/save_post', 'updateLessonMeta', 1);
- // 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
- function lessonPostLink($post_link, $id = 0 ){
- $post = get_post($id);
- echo get_the_id();
- if (is_object( $post)){
- $course = get_field('course_id');
- if( $course ){
- return str_replace('%course%', sanitize_title(get_the_title($course)), $post_link );
- }
- }
- return $post_link;
- }
- add_filter( 'post_type_link', 'lessonPostLink', 1, 1 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement