Need help with Wordpress Draft post $permalink

actnow

Active member
Oct 19, 2019
126
140
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
 

videva

Member
Aug 25, 2020
75
53
18
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
 

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu