自动删除空自定义域

时间:2012-02-20 作者:Demilio

您在下面看到的代码有时只起作用。我不擅长PHP,所以有人能帮我改进这段代码吗?我在WordPress mu设置的函数php文件中使用它。

最大的问题是它不会删除重复的文件。i、 e.如果我有两个名为Image的自定义字段,其中一个为空,我希望此脚本删除此空字段,但它不起作用

add_action(\'save_post\',\'my_cf_check\');
function my_cf_check($post_id) {

    // verify this is not an auto save routine. 
    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return;

    //authentication checks
    if (!current_user_can(\'edit_post\', $post_id)) return;

    //obtain custom field meta for this post
     $custom_fields = get_post_custom($post_id);

    if(!$custom_fields) return;

    foreach($custom_fields as $key=>$custom_field):
        //$custom_field is an array of values associated with $key - even if there is only one value. 
        //Filter to remove empty values.
        //Be warned this will remove anything that casts as false, e.g. 0 or false 
        //- if you don\'t want this, specify a callback.
        //See php documentation on array_filter
        $values = array_filter($custom_field);

        //After removing \'empty\' fields, is array empty?
        if(empty($values)):
            delete_post_meta($post_id,$key); //Remove post\'s custom field
        endif;
    endforeach; 
    return;
} 

2 个回复
SO网友:Stephen Harris

我相信这是我对你之前问题的解决方案。

我在回答中提到,代码没有处理具有多个值的键的偶然性,只有其中一些值是空的。原因是WordPress如何对待delete_post_meta...

使用时delete_post_meta, 前两个参数指定自定义字段的post和键。您有第三个可选参数,可用于指定键值对。所以

//Delete all post meta with key $key    
delete_post_meta($post_id,$key)
//Delete all post meta with key $key AND value $value
delete_post_meta($post_id,$key,$value)
问题是如果$value 为空,它将其视为第一个案例,并删除该帖子和键的所有帖子元。

这里有一个变通方法(也许非基于WordPress的方法更有效),逻辑如下;

识别具有空值的键检索所有非空值删除键,重新插入键,仅添加非空值编辑的foreach循环;

foreach($custom_fields as $key=>$values):
    //$values is an array of values associated with $key - even if there is only one value. 
    //Filter to remove empty values.
    //Be warned this will remove anything that casts as false, e.g. 0 or false 
    //- if you don\'t want this, specify a callback.
    //See php documentation on array_filter
    $nonemptyvalues = array_filter($values);

    //If the $nonemptyvalues doesn\'t match $values then we removed an empty value(s).
    if($nonemptyvalues!=$values):
         //delete key
         delete_post_meta($post_id,$key);

         //re-add key and insert only non-empty values
         foreach($nonemptyvalues as $nonemptyvalue){
             add_post_meta($post_id,$key,$nonemptyvalue);
         }
    endif;  
endforeach;
上述内容未经测试

SO网友:Evan Mattson

看看你是怎么想的:

add_action(\'save_post\',\'my_cf_check\');
function my_cf_check($post_id) {

    // verify this is not an auto save routine. 
    if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ) return;

    //authentication checks
    if (!current_user_can(\'edit_post\', $post_id)) return;

    //obtain custom field meta for this post
    $custom_fields = get_post_custom($post_id);

    if( !$custom_fields ) return;

    foreach ( $custom_fields as $key => $val_arr ):

        if ( empty($val_arr) )
            delete_post_meta($post_id, $key); // if it\'s empty, delete the field.

        elseif ( is_array($val_arr) && 1 < count($val_arr) ) {

            foreach ($val_arr as $value) {
                // check each value, and delete the post meta if it\'s empty
                if ( empty( $value ) )
                    delete_post_meta($post_id, $key, $value);

            }

        } else {
            // it\'s an array with a single non-empty value, or it\'s a non-empty string
        }

    endforeach;

} 

结束