0

仅当高级自定义字段的内容等于 0 时,我将如何调用此函数

 function updateNumbers() {
     global $wpdb;  
     $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb-posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post'";  
     $pageposts = $wpdb->get_results($querystr, OBJECT);  
     $counts = 1440 ;  
     if ($pageposts):  
       foreach ($pageposts as $post):
         setup_postdata($post);  
         $counts++;  
         add_post_meta($post->ID, 'incr_number', $counts, true);  
         update_post_meta($post->ID, 'incr_number', $counts);  
       endforeach;  
     endif;     remove_action ( 'publish_post', 'updateNumbers' ); 
 }     

 add_action ( 'publish_post', 'updateNumbers' );

在此先感谢男孩和女孩!

++++++编辑++++++

为了澄清,这应该在上下文中描述函数:

div 由 ACF 插件控制的 wordpress 字段填充:

<?php $post_id = get_field('job-number'); ?>
div class="postID"><?php echo $post_id ?></div>

如果这个值等于 0 那么我想运行这个函数:

 function updateNumbers() {
     global $wpdb;  
     $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type =
 'post'";  
     $pageposts = $wpdb->get_results($querystr, OBJECT);  
     $counts = 1440 ;  
     if ($pageposts):  
     foreach ($pageposts as $post):
     setup_postdata($post);  
     $counts++;  
     add_post_meta($post->ID, 'incr_number', $counts, true);  
     update_post_meta($post->ID, 'incr_number', $counts);  
     endforeach;  
     endif; }

 add_action ( 'publish_post', 'updateNumbers' );
4

1 回答 1

0

像下面这样的东西似乎是你所追求的......

<?php
    $key = 'field_name';
    $meta = get_post_meta($post->ID, $key, true);
    if ($meta == '0') {
        updateNumbers();
    }
?>
于 2013-01-11T17:01:51.683 回答