0

I have a query that runs through a set of posts with the post type location. It returns them broken down by a custom field value (in this case, the state in which the location is located). For some reason, just one post is being duplicated on the page. When I use the_content or apply_filters('the_content', $content);, it duplicates that one post. When I use get_the_content, it returns the post content normally, but it doesn't have the formatting that I need. What in my code could be causing this?

$state_posts = array();

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

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);
    $content = apply_filters('the_content', get_post_field('post_content', $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>
    <div><?php echo $content; ?></div>
  </div>
</div>
<?php
   }
   wp_reset_postdata();
}

Here is the page in question: https://dare2sharelive.org/locations/?fwp_states=missouri

And yes, I have checked if anything is odd about the two posts in particular, and they're formatted exactly the same as all the other posts.

Edit: here's the query just in case something in there is the issue.

<?php
return array(
    'post_type' => 'location',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'order' => 'ASC',
    'meta_key' => 'state',
    'orderby' => array(
        'meta_value' => 'ASC',
        'title' => 'ASC',
    ),
);
10
  • 1
    Is it possible the one that's duplicating itself has a revision, and none of the other posts do? You might check your initial query and make sure it's searching for 'post-status' => 'publish' to rule it out.
    – WebElaine
    Commented Mar 20, 2018 at 19:03
  • 1
    Do a var_dump( $state_posts ) after the while loop. Does it contain the expected results? Commented Mar 20, 2018 at 19:11
  • 1
    I'd avoid using the variable named $post in the while loop; rename it to something unique like $state_post. Also add wp_reset_postdata(); after the while loop. Commented Mar 20, 2018 at 19:27
  • 1
    I should have worded that differently. Instead of $state_posts[$state][] = $post; I'd do $state_posts[$state][] = $query->post; Commented Mar 20, 2018 at 19:46
  • 1
    Oh, my bad. Well that didn't change anything, either. I'll still leave that change since it's a better practice, though.
    – KreigD
    Commented Mar 20, 2018 at 19:49

1 Answer 1

0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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