简体   繁体   中英

Looping through all posts shows duplicates in Wordpress

Myself and an SEO guy I am working with want to generate our own sitemaps(for certain reasons). To do so, obviously I need to loop through all of the posts and pages within the wordpress installation. I wrote a quick function and uploaded it to the server on a live site to see if it worked, and it does.

The strange thing however, is when I attempt to test this same function on a local install of wordpress(using WAMP), it prints out some post names multiple times(aka duplicates). I just wanted to know if someone here knew of a bug that might be occurring, or if my code needs to change. For example, my code might work on the live site now, but I'd rather not be faced with this issue later on when more posts are added, and I start seeing duplicates in the XML file.

Anyways, here's my code:

<?php
$posts = new WP_Query('post_type=any&posts_per_page=-1&post_status=publish');
$posts = $posts->posts;

// Loop through each post, and echo out the url and it's amp extension.
foreach($posts as $post) {

    if($post->post_type == 'post') {
       $permalink = get_permalink($post->ID);
       echo "\n\t<url>\n\t\t<loc>{$permalink}</loc>\n\t</url>";
       echo "\n\t<url>\n\t\t<loc>{$permalink}amp/</loc>\n\t</url>";
    }
}

As I mentioned, it works on a live site, and does not print duplicates. So you don't have to worry yourself with the syntax of the echo statements, here's what will print for each post on the site:

<url>
   <loc>https://www.somedomain.com/post1/</loc>
</url>

<url>
   <loc>https://www.somedomain.com/post1/amp/</loc>
</url>

Anyone have an opinion on this?

Figured out a solution that uses WP query for the data I need to retrieve, then I just use regular PHP to handle the rest. Might be unnecessary, but I did not feel like wasting any more of my time roaming through the useless Wordpress docs.

I instantiate an empty array at the beginning of the function, and then loop through all of the posts, extracting their ids, and pushing them into to the array I created.

Once the while loop ends, I loop through my array, and print out the xml needed for each post. Pretty simple, and does NOT create duplicates.

$xml = "";
$posts = array();

$q = new WP_Query(array(
    'post_type' => 'any',
    'post_status' => 'publish',
    'posts_per_page' => 50000,
    'has_password' => false,
));

// Loop through all posts, and push each id to the new posts array
if ($q->have_posts()) {
    while ($q->have_posts()) {
        $q->the_post();

        if(get_post_type() == 'post') {
          $id = get_the_ID();
          array_push($posts, $id);
        }
    }
}

// Loop through posts array and append the posts info into xml format
foreach($posts as $post) {
  $xml .= "<url>\n";
  $xml .= "\t<loc>\t\n";
  $xml .= "\t\t" . esc_url(get_permalink($post));
  $xml .= "\n\t</loc>\t";
  $xml .= "\n</url>\n\n";
}

I left out the amp part here, but yeah, in my code I also print out an amp extension for each post as well. Final results print out like so.

<url>
    <loc>   
        http://www.domainname.com/blog/article1
    </loc>  
</url>

<url>
    <loc>   
        http://www.domainname.com/blog/article2
    </loc>  
</url>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM