admin管理员组文章数量:1277900
I wish to change the custom taxonomy for the custom post type after a certain time similar to post expiration.
Custom taxonomy: current-status
term-id: 368
post-type: job
I have created the function wp_set_post_categories
and wish to include it in the post-job template in the generatepress child theme after generate_do_template_part( 'page' );
.
Does this create any issues?
$nvs_taxonomy = 'custom-status';
//expiration time
$expirationtime = get_post_meta($post->ID, 'expiration', false);
if( count( $expirationtime ) != '' ) {
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween >= 0 ) {
echo 'This post will expire on ' .$expirestring.'';
the_content();
} else {
function set_new_category( $post_id ) {
//Define new category by ID
$new_category = array( 368 );
$old_term = wp_get_post_terms( $post_id, $nvs_taxonomy );
if( is_array( $old_term ) && ! empty( $old_term ) ) {
if ( in_array( 'custom-status', $old_term ) {
wp_set_post_categories( $post_id, $new_category, $append = true );
}
}
}
}
}
Following the advice of Mr. @Buttered_Toast I have modified the code little bit to use timestamp data directly. However, in that, I have noticed a "PHP Warning: A non-numeric value encountered in /single-job.php on line 34"
if ( generate_has_default_loop() ) {
while ( have_posts() ) :
the_post();
$expire_timestamp = rwmb_meta( 'j_post_expire' );// line 31
$current_time = time(); // line 32
// Ensure the timestamp parsed correctly.
if ( $expire_timestamp ) {
$seconds_between = $expire_timestamp - $current_time; //line 34
if ( $seconds_between >= 0 ) {
generate_do_template_part( 'single' );
} else {
wp_set_post_terms( $post->ID, array ( 368 ), 'current-status', true );
wp_remove_object_terms( $post->ID, array ( 367 ), 'current-status' );
generate_do_template_part( 'single' );
}
} else {
generate_do_template_part( 'single' );
}
endwhile;
}
To overcome the non-numeric error, I have initialized the variable ($seconds_between) as an integer.
$seconds_between = 0; //intialize as integer after line 32
In other way, we can define other variables ($current_time and $expire_timestamp) with (int)
$seconds_between = (int)$expire_timestamp - (int)$current_time;//declaring variables as integer
- If there is any better way, please share to correct it
I wish to change the custom taxonomy for the custom post type after a certain time similar to post expiration.
Custom taxonomy: current-status
term-id: 368
post-type: job
I have created the function wp_set_post_categories
and wish to include it in the post-job template in the generatepress child theme after generate_do_template_part( 'page' );
.
Does this create any issues?
$nvs_taxonomy = 'custom-status';
//expiration time
$expirationtime = get_post_meta($post->ID, 'expiration', false);
if( count( $expirationtime ) != '' ) {
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween >= 0 ) {
echo 'This post will expire on ' .$expirestring.'';
the_content();
} else {
function set_new_category( $post_id ) {
//Define new category by ID
$new_category = array( 368 );
$old_term = wp_get_post_terms( $post_id, $nvs_taxonomy );
if( is_array( $old_term ) && ! empty( $old_term ) ) {
if ( in_array( 'custom-status', $old_term ) {
wp_set_post_categories( $post_id, $new_category, $append = true );
}
}
}
}
}
Following the advice of Mr. @Buttered_Toast I have modified the code little bit to use timestamp data directly. However, in that, I have noticed a "PHP Warning: A non-numeric value encountered in /single-job.php on line 34"
if ( generate_has_default_loop() ) {
while ( have_posts() ) :
the_post();
$expire_timestamp = rwmb_meta( 'j_post_expire' );// line 31
$current_time = time(); // line 32
// Ensure the timestamp parsed correctly.
if ( $expire_timestamp ) {
$seconds_between = $expire_timestamp - $current_time; //line 34
if ( $seconds_between >= 0 ) {
generate_do_template_part( 'single' );
} else {
wp_set_post_terms( $post->ID, array ( 368 ), 'current-status', true );
wp_remove_object_terms( $post->ID, array ( 367 ), 'current-status' );
generate_do_template_part( 'single' );
}
} else {
generate_do_template_part( 'single' );
}
endwhile;
}
To overcome the non-numeric error, I have initialized the variable ($seconds_between) as an integer.
$seconds_between = 0; //intialize as integer after line 32
In other way, we can define other variables ($current_time and $expire_timestamp) with (int)
$seconds_between = (int)$expire_timestamp - (int)$current_time;//declaring variables as integer
- If there is any better way, please share to correct it
1 Answer
Reset to default 1You've got a number of issues with you code...
if (is_array($expirationtime))
is redundant becauseget_post_meta
will always return an array if the third parameter is false.implode($expirationtime);
requires a separator, e.g.implode(' ', $expirationtime);
.strtotime
can fail to parse your input resulting in a false value. You will get an error when trying to performfalse - time()
.- You're defining a function with
function set_new_category() {}
but not using it. wp_set_post_categories
is for setting terms for the category taxonomy. For custom taxonomies, usewp_set_post_terms
instead.
With all of the above fixed, you should end up with something like...
$expiration_time = get_post_meta( $post->ID, 'expiration', false );
if ( count( $expiration_time ) ) {
$expire_string = implode( ' ', $expiration_time );
$expire_timestamp = strtotime( $expire_string );
// Ensure the timestamp parsed correctly.
if ( $expire_timestamp ) {
$seconds_between = $expire_timestamp - time();
if ( $secondsbetween >= 0 ) {
echo 'This post will expire on ' . $expire_string;
the_content();
} else {
wp_set_post_terms( $post->ID, [ 368 ], 'custom-status', true );
}
}
}
This isn't a complete answer but should definitely point you in the right direction.
本文标签: Change custom taxonomy after certain time – CPT template
版权声明:本文标题:Change custom taxonomy after certain time – CPT template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741271299a2369351.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论