Tommy Wilkey
Tommy Wilkey

Reputation: 137

Having an issue with Wordpress save_post

I am having an issue where when I save a post under a custom post type I created, tb_template, The below code runs fine except rather than creating a new post with the post_type tb_table_cache it is setting the post type to tb_template. I do not understand why this is happening because any place in this code where I am setting the post_type I have it hardcoded to tb_table_cache so I have no idea why it would not be saving the post_type as that.

One thing to note, the code is identical on both, but this issue is happening on my live site and not on my dev site.

Any help is appreciated.

This is ran under the save_post hook.

function build_template_table_cache( $post_id ) {

    $tb_tables = get_posts(array(
        'post_type' => 'tb_table',
        'numberposts' => -1,
        'post_status' => 'publish'
    ));

    if($tb_tables) {
      foreach($tb_tables as $tb_table) {
      $tb_table_template = get_field('tb_table_template', $tb_table->ID);
            if($tb_table_template) {

        if($tb_table_template->ID == $post_id) {
                  self::build_table_cache($post_id, $tb_table->ID, true);
        }

              wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
            }
      }
  }

}

function build_table_cache($post_id, $thispostid = 0, $fromtemplate = false) {
$used_postid = $post_id;
if($fromtemplate) {
      $used_postid = $thispostid;
    }

$post_type = get_post_type($used_postid);
if ( "tb_table" != $post_type || wp_is_post_revision( $used_postid ) ) { return; }

$item_args = array();
$item_args['post_type'] = 'tb_table';
$item_args['post_status'] = 'publish';
$item_args['posts_per_page'] = 1;
$item_args['p'] = $used_postid;

$item_data = new WP_Query($item_args);
if ( $item_data->have_posts() ) {
      while ($item_data->have_posts()) {
        $item_data->the_post();
    $current_post_title = get_the_title();

            $template_object = get_field('tb_table_template');
            if($template_object) {
              $template_type = get_field('tb_table_template_type', $template_object->ID);
      if (empty($template_type)) { $template_type = "normal"; }

          $template = $template_object->post_content;
                $tb_plugin_url = plugin_dir_url(dirname(__FILE__));

                include(plugin_dir_path( dirname( __FILE__ ) ) . "includes/inc/function-table-builder-".$template_type.".php");

                $version_args = array(
                    'post_status'    => 'publish',
                  'post_type'      => 'tb_table_cache',
            'meta_key'       => 'tb_cache_version',
            'orderby'        => 'meta_value_num',
            'order'          => 'DESC',
            'posts_per_page' => 1,
                  'meta_query'   => array(
              array(
                'key' => 'tb_cache_parent',
                'value' => $used_postid,
                'compare' => '=',
              )
                  )
                );
                $version_query = new WP_Query($version_args);
                if ( $version_query->have_posts() ) {
                    while ($version_query->have_posts()) {
                        $version_query->the_post();
          $prev_version = get_post_meta(get_the_ID(), 'tb_cache_version', true);
          if(empty($prev_version)) { $prev_version = 0; }
          if($prev_version < 1) {
            $new_version = 1;
          } else {
            $new_version = $prev_version + 1;
          }
                  }
                } else {
                    $new_version = 1;
      }

                $cache_args = array(
                    'post_status'    => 'publish',
                    'post_type'      => 'tb_table_cache',
                    'post_author'    => get_current_user_id(),
                    'post_title'     => $current_post_title . " - Cache",
                    'post_content'   => $template,
                'meta_input'     => array(
                'tb_cache_parent'  => $used_postid,
                  'tb_cache_version' => $new_version,
                ),
                );

                $cache_post_id = wp_insert_post( $cache_args );
    } // if($template_object)

  } // while ($item_data->have_posts())
    } // if ( $item_data->have_posts() )

} // function build_table_cache( $post_id )

Here is the creation of the post type.

public function tb_table_cache_post_type() {
    register_post_type(
    'tb_table_cache',
    array(
        'labels' => array(
            'name' => __('Table Caches'),
            'singular_name' => __('Table Cache'),
            'add_new_item' => __('Add New Table Cache'),
            'edit_item' => __('Edit Table Cache'),
            'view_item' => __('View Table Cache'),
            'search_items' =>  __('Search Table Caches'),
            'not_found' => __('No Table Caches found!'),
            'not_found_in_trash' => __('No Table Caches found in trash'),
            'menu_name' => __('Table Caches'),
            'all_items' => __('All Table Caches')
        ),
        'description' => 'I15 Table Caches',
        'public' => false,
        'show_ui' => true,
        'menu_position' => 20,
        'hierarchical' => true,
        'supports' => array('title', 'editor'),
        //'show_in_nav_menus' => false,
        'show_in_menu' => 'i15-table-builder',
        'can_export' => true,
        'has_archive' => false,
        'exclude_from_search' => true
    ));

}

Upvotes: 1

Views: 105

Answers (1)

Tommy Wilkey
Tommy Wilkey

Reputation: 137

I figured out the issue. The problem was that we have a plugin installed called Post Type Switcher and that plugin used a filter to change the post type. So I just added my own filter before the insert function and then removed that filter after the insert function.

For anyone curious.

add_filter( 'wp_insert_post_data', array( $this, 'tb_cache_override_type' ), 99, 2 );
$cache_post_id = wp_insert_post( $cache_args );
remove_filter( 'wp_insert_post_data', array( $this, 'tb_cache_override_type' ), 99, 2 );

Upvotes: 1

Related Questions