When you use return in your function, the rest of the function will not run. It work somehow like exit();
or die();
, but it won’t terminate the entire script, only ends that particular piece of code in your function, and return a value if necessary.
You should save your content into a string, and then return it all after you are finished.
So, that’s how your code should look like:
add_filter('the_content', 'copy_content');
function copy_content( $content )
{
$post_type = get_post_type();
$args = array(
'post_type' => 'custom',
'post_status' => 'publish',
'posts_per_page' => -1
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
// Create an empty string
$data="";
while($post_query->have_posts() ) {
$post_query->the_post();
$target_type = get_post_meta(get_the_ID(), "target_type")[0];
if($target_type == $post_type){
// Append each post's content to it
$data .= get_the_content();
}
}
}
wp_reset_postdata();
// Return both content and our custom data altogether
return $content . $data;
}