同时发布多个帖子时未重新生成PHP变量

时间:2021-05-17 作者:UgKnu

上下文:我正在构建一个自动化系统,该系统将从数据库中获取记录,并立即将其作为CPT整体发布。当它们被发布时,我在函数中有一个函数。一个子主题的php文件,应该自动为帖子设置两个类别,a“;“固定”;一个是“人”,另一个是基于一列(categ),这是随邮件发送的记录的一部分。

我的问题是,第二个类别(应该根据记录的内容进行更改)被永久设置为所有正在处理的第一个职位。有没有办法修改下面的代码,以便更好地指定它应该返回在该特定记录中找到的类别?使用“save\\u post”挂钩同时发布多篇文章是否可能有问题?

Content in functions.php

function post_auto_categ( $post_ID ) 
{
    global $wpdb;
    $user_ID = get_current_user_id();
    $post_type = \'people\';

    $categname = $wpdb->get_var("SELECT categ FROM wp_people_posting");
    $categnum = $wpdb->get_var("SELECT term_id FROM wp_terms WHERE name =\'" . $categname ."\'");

    $peoplenum = $wpdb->get_var("SELECT term_id FROM wp_terms WHERE name =\'People\'");
    $post_categories=array($peoplenum, $categnum);

    if(get_post_type($post_ID)==$post_type) 
    {
        wp_set_post_categories( $post_ID, $post_categories );
    }
   return $post_ID;
}
add_action( \'save_post\', \'post_auto_categ\' );

Categories:

enter image description here

Attribute with category content:

enter image description here

如有任何建议,将不胜感激。非常感谢。

Edit for clearer context:

我在phpMyAdmin中有一个名为“wp\\u people\\u posting”的表。enter image description here

此表中的记录是通过一个单独的插件上载的,该插件从Google Drive中获取记录,并将其一次插入此表中。

此表中的每条记录都是发布到网站上的内容,每条记录一条帖子,如下所示。

enter image description here

然后,所有帖子分为两类,其中一类始终命名为;“摘要”;(在最近的一次更新中更改了前面提到的人)和另一个人,这取决于“categ”列的内容。相同的类别名称已作为类别插入网站。

enter image description here

是否有一种方法可以调用一个函数,根据特定帖子/记录的“categ”列的内容为每个帖子设置第二个类别?

Aka,而不是所有人都有相同的;“数据科学”;(如第4幅图所示),我想分别设置它们,如下所示。

Record in Database

这将反映为帖子中列出的类别

Set category for post

Edit 2: Content in Publication Plugin

下面是我用来创建CPT的整个插件,从表中获取详细信息并插入它们:

if(!function_exists(\'add_action\'))
{
    echo \'ERROR: ABSPATH UNDEFINED. Access to this file is not allowed.\';
    exit;
}

function create_abstract_cpt() {

    $labels = array(
        \'name\' => _x( \'Abstracts\', \'Post Type General Name\', \'textdomain\' ),
        \'singular_name\' => _x( \'Abstract\', \'Post Type Singular Name\', \'textdomain\' ),
        \'menu_name\' => _x( \'Student Abstracts\', \'Admin Menu text\', \'textdomain\' ),
        \'name_admin_bar\' => _x( \'Abstract\', \'Add New on Toolbar\', \'textdomain\' ),
        \'archives\' => __( \'Abstract Archives\', \'textdomain\' ),
        \'attributes\' => __( \'Abstract Attributes\', \'textdomain\' ),
        \'parent_item_colon\' => __( \'Parent Abstract:\', \'textdomain\' ),
        \'all_items\' => __( \'All Abstracts\', \'textdomain\' ),
        \'add_new_item\' => __( \'Add New Abstract\', \'textdomain\' ),
        \'add_new\' => __( \'Add New\', \'textdomain\' ),
        \'new_item\' => __( \'New Abstract\', \'textdomain\' ),
        \'edit_item\' => __( \'Edit Abstract\', \'textdomain\' ),
        \'update_item\' => __( \'Update Abstract\', \'textdomain\' ),
        \'view_item\' => __( \'View Abstract\', \'textdomain\' ),
        \'view_items\' => __( \'View Abstracts\', \'textdomain\' ),
        \'search_items\' => __( \'Search Abstract\', \'textdomain\' ),
        \'not_found\' => __( \'Not found\', \'textdomain\' ),
        \'not_found_in_trash\' => __( \'Not found in Trash\', \'textdomain\' ),
        \'featured_image\' => __( \'Featured Image\', \'textdomain\' ),
        \'set_featured_image\' => __( \'Set featured image\', \'textdomain\' ),
        \'remove_featured_image\' => __( \'Remove featured image\', \'textdomain\' ),
        \'use_featured_image\' => __( \'Use as featured image\', \'textdomain\' ),
        \'insert_into_item\' => __( \'Insert into Abstract\', \'textdomain\' ),
        \'uploaded_to_this_item\' => __( \'Uploaded to this Abstract\', \'textdomain\' ),
        \'items_list\' => __( \'Abstracts list\', \'textdomain\' ),
        \'items_list_navigation\' => __( \'Abstracts list navigation\', \'textdomain\' ),
        \'filter_items_list\' => __( \'Filter Abstracts list\', \'textdomain\' ),
    );
    $args = array(
        \'label\' => __( \'Abstract\', \'textdomain\' ),
        \'description\' => __( \'List of all the abstracts submitted by students from the Faculty of ICT\', \'textdomain\' ),
        \'labels\' => $labels,
        \'menu_icon\' => \'dashicons-text\',
        \'supports\' => array(\'title\', \'editor\', \'excerpt\', \'thumbnail\', \'author\', \'post-formats\', \'custom-fields\'),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'menu_position\' => 5,
        \'show_in_admin_bar\' => true,
        \'show_in_nav_menus\' => true,
        \'can_export\' => true,
        \'has_archive\' => true,
        \'hierarchical\' => false,
        \'exclude_from_search\' => false,
        \'show_in_rest\' => true,
        \'publicly_queryable\' => true,
        \'capability_type\' => \'post\',
        \'taxonomies\' => array( \'category\', \'post_tag\' ),
    );
    flush_rewrite_rules();
    register_post_type( \'abstract\', $args );

}
add_action( \'init\', \'create_abstract_cpt\', 0 );

add_action( \'wp\', \'insert_into_cpt\');

function verify_existing_abstract_in_cpt() 
{

    $id_arrays_in_cpt = [];
    $args = array(
        \'post_type\'      => \'abstract\',
        \'posts_per_page\' => -1,
    );

    $loop = new WP_Query($args);
    while ( $loop->have_posts() ) {
        $loop->the_post();
        $id_arrays_in_cpt[] = get_post_meta( get_the_ID(), \'abstract_id\', true);
    }
    return $id_arrays_in_cpt;
}

function query_abstract_post_table( $available_in_cpt ) 
{
    global $wpdb;
    $table_name = $wpdb->prefix . \'abstract_posts\';
    if ( NULL === $available_in_cpt || empty($available_in_cpt) || 0 === $available_in_cpt) {
        $sql = "SELECT * FROM $table_name";
    } else {
        $ids = implode( ",", $available_in_cpt);
        $sql = "SELECT * FROM $table_name WHERE abstract_id NOT IN ($ids)";
    }

    $results = $wpdb->get_results( $sql );
    return $results;
}

function insert_into_cpt()
{
    $available_in_cpt = verify_existing_abstract_in_cpt();
    $database_results = query_abstract_post_table( $available_in_cpt );

    if (NULL === $database_results || empty( $database_results ) || 0 === $database_results) 
    {
        return;
    }

    //Insert into CPT
    foreach($database_results as $result) 
    {
        //Create post object
        $abstract_details = array(
            \'post_title\' => $result->title,
            \'meta_input\' => array(
                \'abstract_id\' => $result->abstract_id,
                \'title\' => $result->title,
                \'author\' => $result->author,
                \'supervisor\' => $result->supervisor,
                \'cosupervisor\' => $result->cosupervisor,
                \'course\' => $result->course,
                \'categ\' => $result->categ,
                \'writeup\' => $result->writeup,
                \'firstabstractimage\' => $result->firstabstractimage,
                \'firstabstractimagecaption\' => $result->firstabstractimagecaption,
                \'secondabstractimage\' => $result->secondabstractimage,
                \'secondabstractimagecaption\' => $result->secondabstractimagecaption,
                \'ref\' => $result->ref,
            ),
            \'post_type\'   => \'abstract\',
            \'post_status\' => \'publish\',
        );
        wp_insert_post( $abstract_details );
    }
}
然后由content.php 文件并直接添加到其中,例如,esc_html(get_post_meta(get_the_ID(), \'firstabstractimage\', true)) (我知道将content.php更改为CPT内容并不理想,很快就会修改,以从archive-abstracts.php 文件)

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

你不需要save_post

当你打电话的时候wp_insert_post, 它将返回post ID或错误对象。我们可以使用它来分配类别,方法是categ 从…起$result.

我想它会像这样:

$post_id = wp_insert_post( $abstract_details );
if ( ! is_wp_error( $post_id ) ) {
    wp_set_object_terms( $post_id, $result[\'categ\'], \'category\' );
}
您也可以使用post_category 的参数wp_insert_post 或者tax_input 参数

相关推荐

修改wp_Dropdown_Categories的输出以向每个选项添加术语ID

我一直在搜索关于如何修改wp\\u dropdown\\u categories的输出的文档或示例,以便我可以将术语ID添加到每个选项中。这可能吗?如果不是这样,我唯一的想法就是尝试使用jQuery,它看起来不那么可靠,也不那么快。这是我当前的代码:<div class=\"location-cats-dropdown\"> <form id=\"location-category-select\" class=\"location-category-select\" m