2

For some reason I am unable to output the custom post type content using the following code. what am I missing here? It works fine for get_the_title yet using get_the_content using same parameters yields nothing.

<?php
            $query = new WP_Query( ['post_type' => 'testimonials', 'posts_per_page' => -1 ] );
            foreach($query->get_posts() as $testimonial):
            $meta = get_post_meta($testimonial->ID);
            foreach($meta as &$m){
                if(is_array($m)){
                    $m = $m[0];
                }
            } ?>

            <div class="content"><?=get_the_content($testimonial->ID); ?></div>
            <div class="author">- <?=get_the_title($testimonial->ID); ?> / <span class="company_name"><?=$meta['_testimonial_company_name'] ?></span></div>
            <div class="link"><a href="<?=home_url('/testimonials'); ?>" title="View All Testimonials">View More</a></div>
            <?php endforeach; ?>
  • 1
    Why are you using references &$m? Avoid references like the plague in PHP, also your WP_Query loop looks strange – Tom J Nowell Aug 23 '17 at 16:13
0

As other answers mentioned, you aren't in The Loop so get_the_*() functions aren't going to work properly. Since you're looping over $query->get_posts() you can just use the WP_Post object.

Something like this will work.

foreach($query->get_posts() as $testimonial):
    $meta = get_post_meta($testimonial->ID);
    foreach($meta as &$m){
            if(is_array($m)){
                $m = $m[0];
            }
        }

    <div class="content"><?= do_shortcode($testimonial->post_content); ?></div>
    <div class="author">- <?=$testimonial->post_title; ?> / <span class="company_name"><?=$meta['_testimonial_company_name'] ?></span></div>
    <div class="link"><a href="<?=home_url('/testimonials'); ?>" title="View All Testimonials">View More</a></div>            

<?php endforeach;?>

Depending on your specific situation you may want to wpautop() the content as well. E.g. do_shortcode(wpautop($testimonial->post_content))

If you want to setup a secondary loop, you can; however, you should NOT use setup_postdata() unless you are sure that you aren't working in a nested loop since that modifies the global post object.

0

get_the_content() must be used within a loop, and it requires you to set up the post's data.

You should use setup_postdata( $post ); first, and then you can use get_the_content().

However, I see a couple of issues here:

  • First of all, you can use a loop, instead of foreach.
  • You should never use short PHP tags. I'm surprised how your code runs at the first place.
  • It's better to store your query parameters into an array for readability, and the pass it to WP_Query();.
  • Use the_content() instead of get_the_content(), to make sure the filters are applied too.

Here is an optimized version of your code:

<?php
$args = array( 
    'post_type' => 'testimonials', 
    'posts_per_page' => 100
);
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
        $q->the_post();
        $company_name = get_post_meta( get_the_ID(), '_testimonial_company_name', true );
        ?>
        <div class="content"><?php the_content(); ?></div>
        <div class="author">- <?php the_title(); ?> / <span class="company_name"><?php echo $company_name; ?></span></div>
        <div class="link"><a href="<?php echo home_url( '/testimonials' ); ?>" title="View All Testimonials">View More</a></div><?php
    }
}
?>
  • so would it be foreach($query->get_posts() as $testimonial): setup_postdata( $post ); ? when i add that i get a wordpress Post content, instead of this custom post type post's content – Zach Smith Aug 23 '17 at 15:51
  • I've updated the answer for you. – Jack Johansson Aug 23 '17 at 15:55
  • 1
    @HollerTrain that might work, but it would be non-standard and strange. Use a standard loop instead as shown in the answer, if you need the ID, there's the $post global variable, and get_the_ID(), as well as all the functions that start with the_ which automatically pick up the post ID – Tom J Nowell Aug 23 '17 at 16:17
  • Foreach is a perfectly acceptable way to iterate over a WP_Query. "You should never use short PHP tags" that is incorrect, you should never use <? instead of <?php but short echos (<?=) will always work on modern environments. – TheGentleman Aug 23 '17 at 19:49
  • Not everything is guaranteed to be run on a modern environment. Please don't encourage users toward bad practices. @GentlemanMax – Jack Johansson Aug 23 '17 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.