如何仅在高级自定义字段内容等于0时调用函数?

时间:2013-01-11 16:52:06

标签: php wordpress

如果高级自定义字段的内容等于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' );

先谢谢男女老少!

++++++ EDIT ++++++

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

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

1 个答案:

答案 0 :(得分:0)

以下似乎是你所追求的......

<?php
    $key = 'field_name';
    $meta = get_post_meta($post->ID, $key, true);
    if ($meta == '0') {
        updateNumbers();
    }
?>