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
- 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
- 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?
- PHP/Manual use of images? – 2 Questions
- 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
- Set Primary category using the Yoast SEO plugin
- Hooking new functions to actions + passing parameters
- 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
- Duplicates with WP_Query loop
- Getting my head round WordPress filter
- enqueue hover function
- WooCommerce: Add Payment Gateway Field to Webhooks [closed]
- 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?
- Handling Body class based on Template
- How to load a template-part based on a url wildcard?
- ajax, right way to do it and make it works?
- Sending data from custom inputs in WordPress comment form in the admin notification email
- Reload part of checkout on changed shipping method
- Trying to build simple deposit code that hooks into woocommerce
- Auto-Select Parent Category as Primary
- Remove a div from RSS feed
- Is it possible to intercept all ajax requests and get the parameters and the returns?
- user_profile_update_errors hook not executing
- Set meta field to publish date + 2 weeks
- WordPress Gravatar filter is removing my custom attributes
- Error in custom php function doesn’t exist
- Return a numerical function value in Customizer controls
- Adding widgets to my plugin page instead of WordPress dashboard
- edit_comment_link is not showing for comment author
- Adding function to Genesis genesis_header [closed]
- Modify custom field input data before creating a new post
- Are all hooks/functions tied to Kses meant for sanitization?
- How to remove woocommerce_breadcrumb() from do_action( ‘woocommerce_before_main_content’ ); [closed]
- Should `wp_login` be used since it’s deprecated?
- Omit custom post type from wp-sitemap.xml based on meta key using wp_sitemaps_posts_query_args
- Password minimum length in personal subscription [closed]
- How to use a custom hook
- Run a code only on theme activation only during first activation
- Theme not calling Jquery properly
- Get access to variable from previous pageview, excluding ajax-calls
- WordPress redirect redirecting too many times or not at all
- How to have different site identity logos on each page on Astra Theme [closed]
- Can not add admin notices from the edit_user_profile_update hook (notices not being displayed)?
- Get user custom field value on function.php
- Add new post in existing categories using wp_insert_post
- Returning all values from foreach in function php [closed]
- Generate CSV file and add data as a new wordpress custom post
- Comapare get_user_meta value
- Must filter functions receive all arguments passed to them?
- Dynamically Generate Functions and Hooks
- how to add datas in taxonomy to post with wp_insert_post
- save_post trigerred twice
- Generate a QR code when creating a new WordPress user
- Replace a single variable with add_filter
- Filter wordpress posts without searching the keywords in the post content
- Variation prices breakdown only for single product page
- Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
- Prevent header and footer from loading on specific pages with template or plugin
- Add custom field on admin dashboard comments / reviews
- Contain multiple page templates in one PHP custom template file in WordPress?
- Wp_Schedule_Event every few minutes doesn’t work
- Preserving backslashes in post_content
- Trouble Accessing Gravity Forms API (GFAPI)
- Enqueue assets from multiple directories using add_action/do_action
- Adding new divs with custom classes to products on WooCommerce shop page [closed]
- Call a single function on two different methods with hooks
- Discount in the specific product title using keyword ( Woocoommerce )
- How do I get the value of a current user’s custom field?
- Need to forward Data from WooCommerce Webhook sent to same site WordPress REST API custom endpoint
- 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
- 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
- Send a custom notification to customer on WooCommerce cancelled order status
- Assign new post author IF another user in custom field on post transition
- Can’t send form data to wpdb when URL has query string
- transition_post_status hook, works – but not if the post is new
- php was updated – critical error [closed]
- Facing Problem While Running WordPress Hook For Archive, Categories, Author, Date Pages Only
- Woocommerce – add tracking code to another email template
- Can’t insert into a database wordpress