Post content being duplicated by the_content();

The problem is still present, but I’ve found a workaround for now. When I replaced echo apply_filters('the_content', get_post_field('post_content', $post_id)) with echo wpautop( get_the_content() ), it worked. I also stopped using the variable $post since I wasn’t accessing the global post variable.

So that makes my code look like so:

$state_posts = array();

while ($query->have_posts()) {
    $query->the_post();
    $state = get_post_meta(get_the_ID(), 'state', true);
    // Changed code here
    $state_posts[$state][] = $query->post;
}

wp_reset_postdata();

foreach ($state_posts as $state_post => $state_title) {
?>
<h1 class="state-name"><?php echo esc_html($state_post); ?></h1>
<?php
foreach ($state_title as $listing) {
    setup_postdata($listing);
    $post_id = $listing->ID;
    $title = get_the_title($post_id);
    $distance = facetwp_get_distance($post_id);
    $distance = (false !== $distance) ? round($distance, 1) . ' miles away' : '';
    $coords = get_post_meta($post_id, 'location', true);
?>
<div class="post-item" data-title="<?php echo esc_attr($title); ?>" data-latitude="<?php echo $coords['lat']; ?>" data-longitude="<?php echo $coords['lng']; ?>" data-distance="<?php echo $distance; ?>">
  <div class="post-item-content">
    <h2><?php echo $title; ?></h2>
    // Changed code here
    <div><?php echo wpautop( get_the_content() ); ?></div>
  </div>
</div>
<?php
   }
   wp_reset_postdata();
}

If anyone has any insight into why the problem occurred, that would be awesome. But either way, the posts are loading correctly with this different code. Thanks to everyone who gave input!

Leave a Comment