在发布POST-Functions.php一段时间后设置对象术语。

时间:2021-12-20 作者:praba

几天后,我不得不将一个类别中已发布的帖子更改为另一个类别。我已经为函数创建了以下代码,并将其添加到函数中。子主题中的php文件。但是,我不知道错误是什么。下面的数据显示了基于循环计算的post列表。唯一缺少的函数是wp\\u set\\u object\\u terms。请帮我解决这个问题。

function set_expired_job_categories() {

    $current_time = time();// define timestamp

    // Set our query arguments
    $args = array(
        \'fields\' => \'ids\', // Only get post ID\'s to improve performance
        \'post_type\' => \'job\', // Post type
        \'post_status\' => \'publish\',
        \'posts_per_page\' => 10,
        \'tax_query\' => array(
            \'taxonomy\' => \'current-status\',
            \'field\' => \'slug\',
            \'terms\' => array( \'ongoing\' ),
        ),
    );
     $q = new WP_Query( $args ); //new wp_query

    // Check if we have posts, if not, return false
    if ( !$q )
        return false;

    // standard WP_query
   if( $q->have_posts() ){
        while( $q->have_posts() ){
            $q->the_post();

            //deadline_date saved as timestamp
            $expire_timestamp = rwmb_meta( \'deadline_date\' );
            if ( $expire_timestamp ) {
                $seconds_between = ( (int)$expire_timestamp - (int)$current_time );
                if ( $seconds_between >= 0 ) {
                }else {
                    wp_set_object_terms( $post->ID, array ( 368 ), \'current-status\', true );
                    wp_remove_object_terms( $post->ID, array ( 367 ), \'current-status\' );
                }
            }
        }
    }
    wp_reset_postdata();
}

// hook fires when the Cron is executed
add_action( \'set_job_categories\', \'set_expired_job_categories\' );

// Add function to register event to wp
add_action( \'wp\', \'register_daily_events_job_expiration\');

function register_daily_events_job_expiration() {
    // Make sure this event hasn\'t been scheduled
    if( !wp_next_scheduled( \'set_job_categories\' ) ) {
        // Schedule the event
        wp_schedule_event( time(), \'hourly\', \'set_job_categories\' );
    }
}
我修改如下

$taxonomy = \'current-status\';
$job_expired_id = 368;
$job_ongoing_id = 367;

$postid = get_the_ID();
wp_set_object_terms( $postid, (int)$job_expired_id, $taxonomy, true );
wp_remove_object_terms( $postid, (int)$job_ongoing_id, $taxonomy );

对于这一点,也没有结果。

1 个回复
SO网友:praba

由于它位于functions文件中,我需要设置全局$wp\\u分类法以最初填充分类法数据。此外,我没有使用tag\\u ID,而是用slug进行了修改。这两个更改有助于编写代码。修订后的代码如下所示,以供参考。谢谢大家的努力。

function set_expired_job_categories() {
    
    global $post;
    global $wp_taxonomies;

    $current_time = time();
    $taxonomy = \'current-status\';
    $job_expired_id = \'expired\';
    $job_ongoing_id = \'ongoing\';

    // Set our query arguments
    $args = array(
        \'fields\' => \'ids\', // Only get post ID\'s to improve performance
        \'post_type\' => \'job\', // Post type
        \'post_status\' => \'publish\',
        \'posts_per_page\' => -1,
        \'tax_query\' => array(
            \'taxonomy\' => \'current-status\',
            \'field\' => \'slug\',
            \'terms\' => array( \'ongoing\' ),
        ),
    );
    $job_expiration_query = new WP_Query( $args );

    // Check if we have posts to set categories, if not, return false
    if( $job_expiration_query->have_posts() ){
        while( $job_expiration_query->have_posts() ){
            $job_expiration_query->the_post();

            $postid = get_the_ID();
            $expire_timestamp = rwmb_meta( \'deadline_date\' );

            if ( $expire_timestamp ) {
                $seconds_between = ( (int)$expire_timestamp - (int)$current_time );
                
                if ( $seconds_between >= 0 ) {
                }else {
                    wp_set_object_terms( $postid, (int)$job_expired_id, $taxonomy, true );
                    wp_remove_object_terms( $postid, (int)$job_ongoing_id, $taxonomy );
                }
            }
        }
        wp_reset_postdata();
    }
}

// hook it to low priority value, due to CPT and Taxonomies
add_action( \'set_job_categories\', \'set_expired_job_categories\', 20, 2 );
参考号:https://wordpress.org/support/topic/wp_set_object_terms-in-loop-is-not-work-in-taxonomy-cpt/

相关推荐

Wp-includes/load.pgp无法识别ISPConfig Jailkit用户外壳中的db.php的文件路径

我目前的问题如下。我在主机maschine上有一个本地LAMP环境WP Multisite,我将其迁移到一个使用jailkit进行安全保护的ISPConfig客户端目录(vbox Debian 10测试服务器)。这是在我的测试服务器上,因此还没有在线可用的内容)迁移之后,由于上载文件路径从/var/www/html/wordpress-directory/wp-content/ 到/web/wp-content/.以更正我的wp调试日志中的各种错误。我使用了以下代码:/* Multisite */