WordPress hwk Codes Ajouter des données dans l’objet WP_POST

Ajouter des données dans l’objet WP_POST

5 mai 2018
<?php
add_filter('posts_results', 'hwk_post_object_extend', 10, 2);
function hwk_post_object_extend($posts, $query){
if(empty($posts))
return $posts;
foreach($posts as $post){
// Post Type = post
if(get_post_type($post) != 'post')
continue;
// Add terms to WP_POST
$post->my_terms = false;
if($terms = get_the_terms($post->ID, 'my_taxonomy'))
$post->my_terms = $terms;
// 'Single' view only
if($query->is_single()){
// Add post meta data
$post->my_meta = false;
if($meta = get_post_meta($post->ID, 'my_meta_key', true))
$post->my_meta = $meta;
}
}
return $posts;
}


Konrad Chmielewski

Evangeliste WordPress & Full Stack Developer depuis 10 années.