最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - After creating Custom post type by user delete old one

programmeradmin5浏览0评论

I have created a form where users update their profile. When a profile is created or updated it creates a CPT called course. The url is the permalink + /course/ + the title of the course

/*  CREATING COURSE PAGES FROM USER PROFILES */

function create_course_page( $user_id = '' ) {
   $user = new WP_User($user_id);
   if ( ! $user->ID ) return '';

   // check if the user whose profile is updating has already a post
   global $wpdb;
   $course_post_exists = $wpdb->get_var( $wpdb->prepare(
   "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = 'course' and post_status = 'publish'", $user->course_name
   ) );

  if ( ! in_array('instructor', $user->roles) ) return '';

  $user_info = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user->ID ) );
  $title = $user_info['course_name'];
  // of course create the content as you want
  $content = 'This is the page for: ';
  $content .= $user_info['description_course'];
  $post = array(
    'post_title' => $title,
    'post_name' => $user->course_name,
    'post_content' => $content,
    'post_status' => 'publish',
    'post_type' => 'course'
  );
  if ( $course_post_exists ) {
    $post['ID'] = $course_post_exists;
    wp_update_post( $post );
  } else {
    wp_insert_post( $post );
  }
}

add_action( 'personal_options_update', 'create_course_page' );
add_action( 'edit_user_profile_update', 'create_course_page' );

The problem that I'm facing is that when someone changes the title of the course it creates a new post with this url.

So I want to delete the old post when a title change is done on the course.

NOTE: When changing title of the course it changes the URL, that is why I think it takes like a new post.

I have customised this code a bit to do the job:

/* REMOVE OLD COURSES WHEN UPDATE */
add_action( 'admin_init', 'removing_older_posts' );
function removing_older_posts() {
    $args = array (
        'post_type'         => 'course',
        'author'            => get_current_user_id(),
        'orderby'           => 'date',
        );
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) { //if the current user has posts under his/her name
        $i = 0; //create post counter
        while ( $query->have_posts() ) { //loop through each post
            $query->the_post(); //get the current post
            if ($i > 0) { //if you're not on the first post
                wp_delete_post( $query->post->ID, true ); //delete the post
            }
            $i++; //increment the post counter
        }
        wp_reset_postdata();
    }
}

But it isn't working.

Appreciate any suggestion :)

I have created a form where users update their profile. When a profile is created or updated it creates a CPT called course. The url is the permalink + /course/ + the title of the course

/*  CREATING COURSE PAGES FROM USER PROFILES */

function create_course_page( $user_id = '' ) {
   $user = new WP_User($user_id);
   if ( ! $user->ID ) return '';

   // check if the user whose profile is updating has already a post
   global $wpdb;
   $course_post_exists = $wpdb->get_var( $wpdb->prepare(
   "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = 'course' and post_status = 'publish'", $user->course_name
   ) );

  if ( ! in_array('instructor', $user->roles) ) return '';

  $user_info = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user->ID ) );
  $title = $user_info['course_name'];
  // of course create the content as you want
  $content = 'This is the page for: ';
  $content .= $user_info['description_course'];
  $post = array(
    'post_title' => $title,
    'post_name' => $user->course_name,
    'post_content' => $content,
    'post_status' => 'publish',
    'post_type' => 'course'
  );
  if ( $course_post_exists ) {
    $post['ID'] = $course_post_exists;
    wp_update_post( $post );
  } else {
    wp_insert_post( $post );
  }
}

add_action( 'personal_options_update', 'create_course_page' );
add_action( 'edit_user_profile_update', 'create_course_page' );

The problem that I'm facing is that when someone changes the title of the course it creates a new post with this url.

So I want to delete the old post when a title change is done on the course.

NOTE: When changing title of the course it changes the URL, that is why I think it takes like a new post.

I have customised this code a bit to do the job:

/* REMOVE OLD COURSES WHEN UPDATE */
add_action( 'admin_init', 'removing_older_posts' );
function removing_older_posts() {
    $args = array (
        'post_type'         => 'course',
        'author'            => get_current_user_id(),
        'orderby'           => 'date',
        );
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) { //if the current user has posts under his/her name
        $i = 0; //create post counter
        while ( $query->have_posts() ) { //loop through each post
            $query->the_post(); //get the current post
            if ($i > 0) { //if you're not on the first post
                wp_delete_post( $query->post->ID, true ); //delete the post
            }
            $i++; //increment the post counter
        }
        wp_reset_postdata();
    }
}

But it isn't working.

Appreciate any suggestion :)

Share Improve this question asked Aug 4, 2020 at 15:22 ZaesarZaesar 1013 bronze badges 9
  • Have you considered just updating the title instead so that no new course post is created? – Tom J Nowell Commented Aug 4, 2020 at 15:51
  • Thanks for your response. We'll, that would be a solution... But don't know how
发布评论

评论列表(0)

  1. 暂无评论