2017-12-08 1 views
1

Ich habe ein Problem, wo, wenn ich einen Beitrag unter einem benutzerdefinierten Beitragstyp ich erstellt, tb_template, Der folgende Code läuft gut, außer, anstatt einen neuen Beitrag mit dem post_type tb_table_cache zu erstellen es setzt Post-Typ zu tb_template. Ich verstehe nicht, warum das passiert, weil irgendwo in diesem Code, wo ich den post_type setze, ich es auf tb_table_cache hartcodiert habe, also habe ich keine Ahnung, warum es den post_type nicht als das speichern würde.Ich habe ein Problem mit Wordpress save_post

Eine Sache zu beachten, der Code ist auf beiden identisch, aber dieses Problem passiert auf meiner Live-Site und nicht auf meiner Dev-Website.

Jede Hilfe wird geschätzt.

Dies wird unter dem save_post Hook ausgeführt.

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) 

Hier ist die Erstellung des Post-Typs.

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 
    )); 

} 
+0

Das ist seltsam! Vielleicht haben Sie in der Definition Ihres Post-Typs versehentlich "tb_table_cache" anstelle von "tb_template" im Array arguments verwendet. Können Sie den für die CPT-Erstellung verwendeten Code posten? – codiiv

+0

Post-Typ-Code hinzugefügt –

Antwort

1

Ich erkannte das Problem. Das Problem war, dass wir ein Plugin namens Post Type Switcher installiert hatten und dieses Plugin einen Filter benutzt um den Post-Typ zu ändern. Ich habe also vor der Insert-Funktion einen eigenen Filter hinzugefügt und diesen nach der Insert-Funktion entfernt.

Für alle Neugierigen.

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); 
Verwandte Themen