How to prevent duplicate posts with wp_insert_post on single

How to prevent duplicate posts with wp_insert_post on single.php

I’m getting a really strange thing. I have the following code in my single.php:

<?php
//– Set up post values
$myPost = array(
‘post_status’ => ‘publish’,
‘post_type’ => ‘post’,
‘post_author’ => 2,
‘post_title’ => ‘e3i8ir’,
‘comment_status’ => ‘closed’,
‘ping_status’ => ‘closed’,
‘post_category’ => array(24),
);

//– Create the new post
$newPostID = wp_insert_post($myPost);

?>

By all rights, this should just insert one record, right? When I use this function, however, it creates TWO posts, exactly alike.

Looking at the page, the_content only displays once, so it’s not like the page is displaying multiple times (and therefore causing two wp_insert_post calls).

Does anyone have any ideas what would cause this?

Thanks for the reply! Unfortunately it looks like whatever is causing it to fire twice is not in the single.php page. This is the entire code I’m using on my test page:

<?php get_header(); ?>

<?php $postTest = 0;
echo ‘<p>LOAD: $postTest=’.$postTest.'</p>’; ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php

//– Set up post values
$myPost = array(
‘post_status’ => ‘publish’,
‘post_type’ => ‘post’,
‘post_author’ => 2, // should be this post’s Author
‘post_title’ => ‘e3i8ir’,
‘comment_status’ => ‘closed’,
‘ping_status’ => ‘closed’,
‘post_category’ => array(24), // should be the NEXT category
);

echo ‘<p>BEFORE: $postTest=’.$postTest.'</p>’; //test

//– Create the new post
$newPostID = wp_insert_post($myPost);

$postTest++;
echo ‘<p>AFTER: $postTest=’.$postTest.'</p>’; //test

?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php get_footer(); ?>

Output is:

<p>LOAD: $postTest=0</p>
<p>BEFORE: $postTest=0</p><p>AFTER: $postTest=1</p>

No duplication 😦

Any ideas what could cause this function to fire twice?

Thanks,

I’m getting a similar problem creating posts from db entries.

The following code is inside a foreach loop, within a function in functions.php:

echo $post_content;
$new_post = array(
‘post_title’ => $post_title,
‘post_content’ => $post_content,
‘post_status’ => ‘publish’,
‘post_date’ => date(‘Y-m-d H:i:s’),
‘post_author’ => $user_ID,
‘post_type’ => ‘post’,
‘post_category’ => array($prod_id)
);
$post_id = wp_insert_post($new_post);

The echo displays on-page once per row from db, as it should. But the wp_insert_post() call is creating 3 posts, per row.

This is in FF3.6 mac. After reading above posts, tried it in safari mac, but exactly the same thing happens (3 of each post).

<h3>How to prevent duplicate output of query?</h3>

Ignore the fact that the table only shows a few entries – I haven’t gone through all of the posts to add the meta data that is called into the table yet.

and the code for my sidebar query (which you can see at the top of the sidebar in unstyled fashion) is:

<?php
$args=array(‘posts_per_page’ => -1, ‘post_status’ => ‘publish’, ‘orderby’=> ‘title’, ‘order’ => ‘ASC’, ‘cat’ => get_query_var( ‘cat’ ) );
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {  ?>

<?php
while ($my_query->have_posts()) : $my_query->the_post();
$class = get_post_meta($post->ID, ‘class’, true);
$year = get_post_meta($post->ID, ‘year’, true); ?>

<h3 style=”padding-left:10px;”><a href=”<? the_permalink() ?>” title=”<? the_title() ?>”><?php
$key_1_value = get_post_meta( get_the_ID(), ‘brand_value’, true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
echo $key_1_value;
}
?></a></h3>

<?php endwhile; ?>
<?php }
wp_reset_query();
?>

Thanks for any help! I know I saw this described once on a website, but google has not brought me back to that page again.

<h3>Multiple loops avoiding duplicate posts</h3>

You will need an adaptation of the ‘mullet loop’.

You will need something like:

<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>

// if this is the first post, process any code that is specified in this region
// process any code specified in this region before the content of the first post

<?php the_content(); ?> // display the full content of the first post only

// process any code specified in this region after the content of the first post

<?php elseif ($count < 4) : ?>

// if this is the second or third post, process any code that is specified in this region
// process any code specified in this region before the content of the second or third post

<?php the_excerpt(); ?> // display the excerpt of the second and third posts

// process any code specified in this region after the second and third posts

<?php else : ?>

// if this is fourth or more post, process any code specified in this region
// process any code specified in this region before the content of each post

<?php the_title(); ?> // display only the title for all other posts

// for all posts after the third, process this code below each post

<?php endif; ?>

// for each post, including the first, process any code included here
// any code output will be displayed below the content of every post

<?php endwhile; ?>
<?php endif; ?>

Many thanks you guys! It’s working. For other people, the code is as following:

<?php get_header(); ?>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

<?php query_posts(‘showposts=1’); ?>
<?php $posts = get_posts(‘numberposts=1&offset=0’); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == “1”) { break; } else { ?>

<?php the_title(); ?>
<?php the_excerpt(); ?>

<?php $count1++; } ?>
<?php endforeach; ?>

<div id=”leftpost”>
<?php query_posts(‘showposts=1’); ?>
<?php $posts = get_posts(‘numberposts=1&offset=1’); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == “1”) { break; } else { ?>

<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php $count2++; } ?>
</div>
<?php endforeach; ?>

<div id=”rightpost”>
<?php query_posts(‘showposts=1’); ?>
<?php $posts = get_posts(‘numberposts=1&offset=2’); foreach ($posts as $post) : start_wp(); ?>
<?php static $count3 = 0; if ($count3 == “1”) { break; } else { ?>

<?php the_title(); ?>
<?php the_excerpt(); ?>

<?php $count3++; } ?>
</div>
<?php endforeach; ?>

<ul>
<?php query_posts(‘showposts=7’); ?>
<?php $posts = get_posts(‘numberposts=7&offset=3’); foreach ($posts as $post) : start_wp(); ?>
<?php static $count4 = 0; if ($count4 == “7”) { break; } else { ?>

<li><a class=”” href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>

<?php $count4++; } ?>

<?php endforeach; ?>
</ul>

<?php endwhile; ?>

<?php else : ?>

<h2><?php _e(‘The page you<code>re looking for doesn</code>t exist’, ‘blank’); ?></h2>
<div class=”search-404″>
<?php _e(‘Do you want to search for it?’, ‘blank’); ?><br />
<?php include (TEMPLATEPATH . “/searchform.php”); ?>
</div>

<?php endif; ?>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

<h3>Prevent the Same Post from Showing in Multiple “Highlights” Sections</h3>

I’m trying to help a friend with this problem….

He is using a theme which contains 3 highlights sections on the homepage. The user assigns each Highlights section a specific category in the theme options panel, so each of the 3 sections display the last post from their assigned category.

The problem is that his site only has 4 categories and many of the posts are assigned to all the same categories. Therefore, the same post often appears in 2 or sometimes all 3 of the highlights sections.

Is there code that can be added that will prevent the same post from showing more than once? Something that will say: if post A is displayed in highlights #1, do not show post A in highlights #2 or #3.

The website in question is: http://www.sgpspclub.com/ and the code for the highlights section is:

<!– Highlights // –>
<span class=”heading2″><span>Highlights</span></span>
<ul class=”highlights”>
<?php $highlight1 = new WP_Query(“meta_key=thumbnail&cat=”.get_wpn_config(‘highlights_category_id_1’).”&showposts=1″); while($highlight1->have_posts()) : $highlight1->the_post(); ?>
<li>
<?php if(get_post_meta($post->ID, “thumbnail”, true)) : ?>
<a href=”<?php the_permalink(); ?>” class=”thumbnail”><img src=”<?php echo get_post_meta($post->ID, “thumbnail”, true); ?>” /></a>
<?php endif; ?>
<span class=”title”><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></span>
<span class=”cat”><?php the_category(‘, ‘); ?></span>
</li>
<?php endwhile; ?>

<?php $highlight2 = new WP_Query(“meta_key=thumbnail&cat=”.get_wpn_config(‘highlights_category_id_2’).”&showposts=1″); while($highlight2->have_posts()) : $highlight2->the_post();?>
<li>
<?php if(get_post_meta($post->ID, “thumbnail”, true)) : ?>
<a href=”<?php the_permalink(); ?>” class=”thumbnail”><img src=”<?php echo get_post_meta($post->ID, “thumbnail”, true); ?>” /></a>
<?php endif; ?>
<span class=”title”><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></span>
<span class=”cat”><?php the_category(‘, ‘); ?></span>
</li>
<?php endwhile; ?>

<?php $highlight3 = new WP_Query(“meta_key=thumbnail&cat=”.get_wpn_config(‘highlights_category_id_3’).”&showposts=1″); while($highlight3->have_posts()) : $highlight3->the_post();?>
<li>
<?php if(get_post_meta($post->ID, “thumbnail”, true)) : ?>
<a href=”<?php the_permalink(); ?>” class=”thumbnail”><img src=”<?php echo get_post_meta($post->ID, “thumbnail”, true); ?>” /></a>
<?php endif; ?>
<span class=”title”><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></span>
<span class=”cat”><?php the_category(‘, ‘); ?></span>
</li>
<?php endwhile; ?>

</ul>
<div class=”clear”></div>
<!– // Highlights –>
<?php endif; ?>

Help is greatly appreciated!!

<h3>How to remove double titles</h3>

I just tried the IF statement, but it didn’t seem to work.

In the single.php file, the title tag looks like this:

<h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3>

I changed it as follows, using a page id of 3 for the file I want to modify:

<?php If (!is_single(‘3’)){?><h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3><?php }?>

I then uploaded it into the theme folder.

Am I doing something wrong here?

Actually, that will work if you’re loading just that page. Unfortunately, when your blog is called, it references the Main Index Template (index.php). If you want the header to disappear on the front page, though, you’ll need to find the same section header code in index.php and change it to the following:

<?php if( ! is_front_page()){?><h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3><?php }?>

With both of those changes applied, you shouldn’t have any problems. Unless, of course, we’ve missed something. In that case, feel free to come back and let us know. 🙂

Hi,

Here is the html source of the section on the rendered page:

<li class=”page_item <li class=”page_item page-item-3 current_page_item”><a href=”http://www.antonellasevero.com/joantest&#8221; title=”home”>home</a></li>
<li class=”page_item page-item-2″><a href=”http://www.antonellasevero.com/joantest/informacion/&#8221; title=”biografía”>biografía</a></li>
<li class=”page_item page-item-9″><a href=”http://www.antonellasevero.com/joantest/blog/&#8221; title=”Blog”>Blog</a></li>
</li></ul>

Here is the source of the index.php file:

<?php get_header(); ?>

<div id=”content”>

<div id=”content-left”>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

<div class=”box-left” id=”post-<?php the_ID(); ?>”>

<?php If (!is_front_page()){?><h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3><?php }?>
<div class=”meta”>
<span class=”meta-date”><?php the_time(‘l, F jS, Y’); ?></span> |
<span class=”meta-categories”><?php the_category(‘, ‘); ?></span> |
<span class=”meta-comments”><?php comments_popup_link(__(‘No Comments’), __(‘1 Comment’), __(‘% Comments’)); ?></span>
</div>

<?php the_content(__(‘&rsaquo; Continue reading’)); ?>

<?php the_tags(‘<p class=”tags”>Tags: ‘, ‘, ‘, ‘</p>’); ?>

<div class=”clear”></div></div>

<?php endwhile; ?>

<div class=”box-left navigation”>

<?php next_posts_link(‘&laquo; Previous Entries’) ?> <?php previous_posts_link(‘Next Entries &raquo;’) ?>

</div>

<?php else : ?>

<div class=”box-left”>

<h3>Not found!</h3>
<p><?php _e(‘Sorry, no posts matched your criteria.’); ?></p>
<?php include (TEMPLATEPATH . “/searchform.php”); ?>

</div>

<?php endif; ?>

</div><!– end content-left –>

<?php get_sidebar(); ?>

<div class=”clear”></div>

</div><!– end content –>

<?php get_footer(); ?>

Here is the source of the single.php file:

<?php get_header(); ?>

<div id=”content”>

<div id=”content-left”>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

<div class=”box-left” id=”post-<?php the_ID(); ?>”>

<?php If (!is_single(‘3’)){?><h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3><?php }?>
<div class=”meta”>
<span class=”meta-date”><?php the_time(‘l, F jS, Y’); ?></span> |
<span class=”meta-categories”><?php the_category(‘, ‘); ?></span>
</div>

<?php the_content(); ?>

<?php the_tags(‘<p class=”tags”>Tags: ‘, ‘, ‘, ‘</p>’); ?>

<div class=”clear”></div></div>

<?php comments_template(); ?>

<?php endwhile; ?>

<?php else : ?>

<div class=”box-left”>

<h3>Not found!</h3>
<p><?php _e(‘Sorry, no posts matched your criteria.’); ?></p>
<?php include (TEMPLATEPATH . “/searchform.php”); ?>

</div>

<?php endif; ?>

</div><!– end content-left –>

<?php get_sidebar(); ?>

<div class=”clear”></div>

</div><!– end content –>

<?php get_footer(); ?>

Let me know if you needed something else to see.

Thanks so much!

Antonella,

The only other change I’d recommend is checking is_home in index.php.

Change
<?php if( ! is_front_page()){?><h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3><?php }?>
To:

<?php if(is_front_page() || is_home()){?>
<?php } else { ?>
<h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3>
<?php }?>

In index.php and let me know if that fixed the problem. If not, then something else is going on …

This code will check if the user is on the front page or the home page (which are both the same in your case). If they are on either, then it will skip displaying the title. If they aren’t on either page, then it will display the title.

Antonella,

Try replacing

<?php if(is_front_page() || is_home()){?>
<?php } else { ?>
<h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3>
<?php }?>

With

<?php if(is_home()){?>
<?php } else { ?>
<h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3>
<?php }?>

I figured it out!!! I started from scratch on a different theme and tested one element at a time. I got it to work and then tried it on the theme I wanted (demar), and with just a variation, also got it to work.

The only changes required are in the page.php file (no changes to the index.php or single.php).

If the page.php file has a title tag like this:
<h2><?php the_title(); ?></h2>

replace it with this:

<?php if( ! is_front_page()){?>
<h2><?php the_title(); ?></h2><?php }?>

If the page.php file has a title tag that includes a permalink reference like this:
<h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3>

replace it with this:
<?php if( ! is_front_page()){?><h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3><?php }?>

Somehow the code function didn’t work in my previous post so I will repost that section for clarity:

If the page.php file has a title tag like this:

<h2><?php the_title(); ?></h2>

replace it with:

<?php if( ! is_front_page()){?>
<h2><?php the_title(); ?></h2><?php }?>

If the page.php file has a title tag that includes a permalink reference like this:

<h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3>

replace it with this:

<?php if( ! is_front_page()){?><h3><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3><?php }?>

Hello, I’m after the same thing, deleting double titles. The theme I’m using is Thematic and it doesn’t have a page.php. It looks like the php I need to modify might be on the index.php page. I’m not fluent in php. The two pages I’m trying to eliminate the double titles on is “About” and “Contact”, http://thedadreport.com/about/

Here is my index.php code

<?php get_header(); ?>
<!– “You know what I need? I need a drink. There’s some ice and stuff back there. Why don’t you make us all some old fashioneds? – Tyler Fitzgerald” –>

<div id=”contentwrap”>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class=”entry”>
<h3 <?php if (is_page()) echo ‘style=”margin-bottom: 20px;” ‘ ?>class=”entrytitle” id=”post-<?php the_ID(); ?>”><a title=”Article-Link (Permalink)” href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a> <?php edit_post_link(‘<img class=”editpost” alt=”Edit” src=”‘ . get_bloginfo(‘template_directory’) . ‘/images/edit.gif” />’, ”, ”); ?></h3>
<div class=”entrymeta1″>
<?php

if (! is_page()) {    // Do not display if we are on a page
// Date and author
if (is_single()) { $articledate = get_the_time(‘F j, Y, G:i’) . ‘ Uhr’; } else { $articledate = get_the_time(‘F j, Y’); }
echo ‘<span class=”meta-time”>’ . $articledate . ‘</span><span class=”meta-category”>’; the_category(‘, ‘); echo ‘</span><span class=”meta-author”><a title=”author” href=”‘; the_author_url(); echo ‘”>’; the_author(); echo ‘</a></span>’;
// Comments link
comments_popup_link(‘<span class=”meta-comments”>0 comments</span>’, ‘<span class=”meta-comments”>1 comment</span>’, ‘<span class=”meta-comments”>% comments</span>’);

} // ! is_page()
?>
</div> <!– [entrymeta1] –>

<div class=”entrybody”>
<?php the_content(__(‘Read more &raquo;’));?>
</div> <!– [entrybody] –>

<?php if (is_single()) { ?>

<div class=”entrymeta2″>
<ul>
<?php
// *** Trackback URI: only if ping is enabled
if ( pings_open() ) { ?>
<li><a class=”trackback-leftalign” title=”Trackback-URL for ‘<?php the_title() ?>'” href=”<?php trackback_url() ?>” rel=”nofollow”>Trackback-URL</a></li>
<?php   }
?>

<?php
// *** RSS Comments: only if comments are enabled
if ( comments_open() ) { ?>
<li class=”feed-leftalign”><span title=”Subscribe to comments feed”><?php comments_rss_link(‘RSS feed for comments on this post’) ?></span></li>
<?php    }
?>

<?php
// *** Tags: only if there is any
if ( (function_exists(‘UTW_ShowTagsForCurrentPost’)) ) { ?>
<li class=”utwtags”><span title=”tags”><?php UTW_ShowTagsForCurrentPost(“commalist”) ?></span></li>
<?php   }
?>
</ul>

</div> <!– [entrymeta2] –>

<?php comments_template(); // Get wp-comments.php template ?>

<?php } // is_single() ?>

<!–
<?php trackback_rdf(); ?>
–>
</div> <!– [entry] –>

<?php endwhile; else: ?>
<p><?php _e(‘No Entries found.’); ?></p>
<?php endif; ?>
<p><?php posts_nav_link(‘ — ‘, __(‘&laquo; Previous Page’), __(‘Next Page &raquo;’)); ?></p>

</div> <!– [contentwrap] –>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

<div class=”entry”>
<?php if (!is_page()) { ?>
<h3 <?php if (is_page()) echo ‘style=”margin-bottom: 20px;” ‘ ?>class=”entrytitle” id=”post-<?php the_ID(); ?>”><a title=”Article-Link (Permalink)” href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a> <?php edit_post_link(‘<img class=”editpost” alt=”Edit” src=”‘ . get_bloginfo(‘template_directory’) . ‘/images/edit.gif” />’, ”, ”); ?></h3>
<?php } ?>
<div class=”entrymeta1″>

Leave a comment