You could add the additional check to your foreach loop to see if the post with exactly same slug as the one you’re about to add already exists.
Kinda difficult to tell exactly what you would have to do without seeing your current code.
I’m assuming you know the slug and a type of a post that you’re trying to insert?
You could look up the existing post with that slug first:
$existing_item = get_page_by_path( 'your-post-slug', OBJECT, 'your_post_type');
// null means that there is no post with that slug
if ( null === $existing_item ) {
// proceed with inserting a post
}
Bear in mind that this would affect your page performance even more, as you’re adding additional query to every item in the loop.
Related Posts:
- Create and publish a new post every Monday, listing the post titles and links to the posts published during the previous week
- what is correct way to hook when update post
- How to update custom fields using the wp_insert_post() function?
- Using register_activation_hook in classes
- WordPress and event-driven programming – what is it about?
- Custom plugin: Trying to show saved data on frontend
- Receiving Stripe Webhooks on a wordpress website
- Remove Woocommerce product link
- Check php version before theme activation
- Change the footer text on the login page
- How can I remove “Proudly powered by WordPress” from twentyeleven without modifying footer.php?
- How can I hook into and edit the text of a wp_nav_menu tag?
- How to remove a metabox from menu editor page?
- PHP/Manual use of images? – 2 Questions
- WooCommerce: How to display item meta data in email confirmation using woocommerce_order_item_meta_end
- Check if page parent has certain template
- Multiple wp_schedule_event cron jobs in plugin cause multi-execution
- Real time Duplicate title check
- How to remove “out of stock” variation options from size dropdown in woocommerce?
- Prevent publish status/date saved on transition_post_status hook
- Audio Player not loading when the content is loaded through Ajax, MediaElement.js no applied
- How to Remove a Filter from the Admin List Table?
- Set Primary category using the Yoast SEO plugin
- Hooking new functions to actions + passing parameters
- Woocommerce, recognize the loop of related products
- Find variables available at a given hook
- Passing data between two hooks in separate HTML requests
- Is $hook a global variable in WordPress
- Replace existing content from specific WooCommerce admin orders list column
- Executing `createimagefrompng()` from save_post hook (or equivalent hook)
- Duplicates with WP_Query loop
- Getting my head round WordPress filter
- Delete option value from array using update_option()
- enqueue hover function
- How To Change The Html of Products filtration sidebar in Woocommerce?
- How to set a custom path, for ajax image upload folder in admin?
- Insert all post IDs in new database table
- WooCommerce: Add Payment Gateway Field to Webhooks [closed]
- action hook wp_head higher priority as all other plugins/hooks
- using add_action for a header hook that has an additional parameter
- When using the_author hook, how can I determine the PHP file that generates each call to `the_author()`?
- wp_insert_post not working
- How to call function at the bottom of post using plugin?
- wp_insert_post() is returning the correct post ID, no failure, but the post content does not get updated
- How to add a second ‘place order’ button in WooCommerce [closed]
- How to do multiple searches (with logical OR) in WP_Query in hook pre_get_posts?
- Randomise upload filenames (or another solution to hide the original image URL from theft?)
- Setting up 2 SMTP accounts: 1 for wordpress and 1 for woocommerce
- wp_loaded hook block script enquequing
- Handling Body class based on Template
- How to load a template-part based on a url wildcard?
- Remove an action hook within a Class
- How to pass arguments to add_action() or retrieve return value of called function?
- Can’t get wp_insert_post to work
- Part of title duplicating but not sure how to remove from code – help?
- hook that when page is starting to load
- How to change home or site url using action hooks or filter?
- ajax, right way to do it and make it works?
- Sending data from custom inputs in WordPress comment form in the admin notification email
- Edit incorrect password message WordPress
- Reload part of checkout on changed shipping method
- Getting page / post URL on publish and / or update
- how to call other plugins once custom post has been inserted
- Add class to all meta boxes for a custom post type
- Fatal error when using array_diff() function inside of wp_update_nav_menu hook?
- Adding user meta to post meta wp_insert_post() not working
- Why transition_post_type hook is called twice for the same post?
- Clone the “proceed to cart” button and place it above checkout on the cart page, but only appear display size is 320px
- Is it possible to dupplicate a WordPress post with php?
- How can I move the posted-on date above the post and below the entry-header?
- Passing link parameter values to form
- Front end post submissions do not get submitted in the category
- Post formating on Home page
- WordPress HTML Helper
- calling a function from a class in your template
- Remove duplicates from foreach
- Post content not showing some content
- How to add custom checkout field in user details mail template
- upload images on front by user using form
- Cannot modify header information – headers already sent
- For each loop will not append to the_content hook
- Change Checkout “Place Order” text if cart has a specific product
- Send a custom notification to customer on WooCommerce cancelled order status
- Assign new post author IF another user in custom field on post transition
- PHP – Multiple variables in insert post array
- Can’t send form data to wpdb when URL has query string
- understanding wp_next_scheduled
- Add/alter image attribute before render
- transition_post_status hook, works – but not if the post is new
- php was updated – critical error [closed]
- Guests comment form – prevent duplicate email addresses
- Facing Problem While Running WordPress Hook For Archive, Categories, Author, Date Pages Only
- Woocommerce – add tracking code to another email template
- Is there a hook that I can use when a fatal error occurs?
- Can’t insert into a database wordpress
- Is there a way to pull remotely generated XML, process it, and display it on a Managed WordPress page?
- how to append ACF field data using one of the following filter/ hooks
- Custom filter not affecting gtag script position in WordPress footer
- Responsive images, per image type?
- Add Imports to Existing WordPress Import Map