简体   繁体   English

将meta键添加到所有帖子-自定义帖子类型

[英]add meta key to all posts - custom post type

I actually wrote this function a week ago, but I didn't save it and am having trouble figuring it out again. 我实际上是在一周前编写了此函数,但是我没有保存它,并且在再次确定它时遇到了麻烦。

Here is a different function I wrote that adds the meta key 'orientation' with respective values depending on image size. 这是我写的另一个函数,该函数根据图像大小添加带有相应值的元键“方向”。 I'd like to convert this to a function that I can insert in my functions.php and run once to add the field to all existing posts, then delete it after. 我想将其转换为可以插入我的functions.php中的函数,然后运行一次以将该字段添加到所有现有帖子中,然后将其删除。

I've tried a number of different methods. 我尝试了许多不同的方法。 Any and all help would be greatly appreciated. 任何和所有帮助将不胜感激。

add_action( 'save_post_portfolio', 'add_orientation' );
function add_orientation($post_ID) {
    global $wpdb;

            $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
            $orientation = get_post_meta($post->ID, 'orientation', TRUE);
            if($orientation != '') {
            }
            elseif($post_thumbnail_id) {
            $image = wp_get_attachment_image_src( $post_thumbnail_id, 'full' );
            if($image[1] >= 0 && $image[2] >= 650) {
            add_post_meta($post_ID, 'orientation', 'portrait');
            }
            elseif($image[1] >= 0 && $image[2] <= 650) {
            add_post_meta($post_ID, 'orientation', 'landscape');
            }
        }   

}

I'm not sure, but should work. 我不确定,但是应该可以。 Try this: Main function: 试试这个:主要功能:

function get_post_meta_data($post_id){
global $wpdb;
$wpdb->query("SELECT 'post_id','meta_key','meta_value' FROM $wpdb->postmeta WHERE 'post_id' = $post_id");
foreach($wpdb->last_result as $k => $v){
    //data:
    //$v->meta_key == meta_key of post 
    //$v->meta_value == meta_value of post
    //$post_id == post id
    /*
    Do here what you want
    */
};
}

and once-launch function: 和一次启动功能:

function update_meta_orientation()
{
global $wpdb;
//get all post id:
$wpdb->query("SELECT 'post_id' FROM $wpdb->postmeta");
//call function for each post
foreach($wpdb->last_result as $key => $value)
    {
    get_post_meta_data($value->post_id);
    }
}

Wasn't sure how Sysanin code related to my answer, but I wrote a solution that finally worked 不确定Sysanin代码与我的答案如何相关,但我写了一个最终有效的解决方案

$args = array( 'numberposts' => -1, 'post_type' => 'portfolio' );
$postslist = get_posts( $args );
foreach ($postslist as $post) :  setup_postdata($post);
            $post_id = get_the_ID();
            $post_thumbnail_id = get_post_thumbnail_id( $post_id );
            $orientation = get_post_meta($post_id, 'orientation', TRUE);
            if($orientation =='') {
            if($post_thumbnail_id) {
            $image = wp_get_attachment_image_src( $post_thumbnail_id, 'full' );
            if($image[1] >= 0 && $image[2] >= 650) {
            add_post_meta($post_id, 'orientation', 'portrait');
            }
            elseif($image[1] >= 0 && $image[2] <= 650) {
            add_post_meta($post_id, 'orientation', 'landscape');
            }
        }   
    }
    else {
    }
endforeach;

and then here it is stripped for anyone's use 然后在这里将其剥离以供任何人使用

$args = array( 'numberposts' => -1, 'post_type' => 'cpt' );
$postslist = get_posts( $args );
foreach ($postslist as $post) :  setup_postdata($post);
$post_id = get_the_ID(); // use this variable
// do stuff here          
endforeach;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM