• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

Need help with Wordpress Draft post $permalink

actnow

Active member
Oct 19, 2019
130
209
43
I am using Masterstudy theme, when the course is published as draft we can only view the course details and when we go to lessons we get 404
The draft preview URL is like domain.com/?post_type=stm-courses&p=882&preview=true
The lesson URL is like domain.com/?post_type=stm-courses&p=882882-13 (404)

The problem seems to be premalink

If the course is published
The live URL is like domain.com/courses/web-coding-and-apache-basics/
The lesson URL is like domain.com/courses/web-coding-and-apache-basics/882-13

I tried to add a function
Code:
function draft_permalink( $post_id, $current_permalink) {
  $post = get_post( $post_id );
  if (in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) {
    $my_post = clone $post;
    $my_post->post_status = 'published';
    $my_post->post_name = sanitize_title($my_post->post_name ? $my_post->post_name : $my_post->post_title, $my_post->ID);
    $permalink = get_permalink($my_post);
  } else {
    $permalink = $current_permalink;
  }
  return $permalink;
}

function course_wp_link_query( $results, $query ) {
  foreach ( $results as &$result ) {
    $result['permalink'] = draft_permalink( $result['ID'], $result['permalink'] );       
  }
  return $results;
}

add_filter( 'wp_link_query', 'course_wp_link_query' );

It does not seem to work, any help would be appreciated
 
I am using Masterstudy theme, when the course is published as draft we can only view the course details and when we go to lessons we get 404
The draft preview URL is like domain.com/?post_type=stm-courses&p=882&preview=true
The lesson URL is like domain.com/?post_type=stm-courses&p=882882-13 (404)

The problem seems to be premalink

If the course is published
The live URL is like domain.com/courses/web-coding-and-apache-basics/
The lesson URL is like domain.com/courses/web-coding-and-apache-basics/882-13

I tried to add a function
Code:
function draft_permalink( $post_id, $current_permalink) {
  $post = get_post( $post_id );
  if (in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) {
    $my_post = clone $post;
    $my_post->post_status = 'published';
    $my_post->post_name = sanitize_title($my_post->post_name ? $my_post->post_name : $my_post->post_title, $my_post->ID);
    $permalink = get_permalink($my_post);
  } else {
    $permalink = $current_permalink;
  }
  return $permalink;
}

function course_wp_link_query( $results, $query ) {
  foreach ( $results as &$result ) {
    $result['permalink'] = draft_permalink( $result['ID'], $result['permalink'] );      
  }
  return $results;
}

add_filter( 'wp_link_query', 'course_wp_link_query' );

It does not seem to work, any help would be appreciated
so what do you want? please explain in more detail
 
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock