Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Update postdata without creating duplicate on wp_insert_post from RSS feed

You can save AdvertID with your post and use it to check if the job was imported before.

/**
 * Gets a job by AdvertId.
 * 
 * @param   string          $advert_id  The AdvertId.
 * @return  WP_Post|bool                The job or <false> if no job is found.
 */
function get_job_by_advert_id( $advert_id ) {
    $args = array(
        'post_type' => 'jobs',
        'meta_query' => array(
            'key' => 'advert_id',
            'value' => $advert_id,  
        ),
    );
    $posts = get_posts( $args );

    if (empty($posts)) {
        return false;
    }

    return $posts[0];
}

// Check if the job was imported before.
$job = get_job_by_advert_id( $item->AdvertId );

if ( $job ) {

    // The job was imported before, update it.
    $args = array(
        'ID' => $job->ID,
        'post_content'   => $item->description,
        'post_date'      => $item_date,
        'post_title'     => $item_title,
    );

    $post_id = wp_update_post( $args ); 

} else {

    // The job is new. Insert it.
    $args = array(
      'post_content'   => $item->description,
      'post_date'      => $item_date,
      'post_title'     => $item_title,
      'post_status'    => 'publish',
      'post_type'      => 'jobs',
    );
    $post_id = wp_insert_post($args);   

    // Save the AdvertId for the next import.
    add_post_meta( $post_id, 'advert_id', $item->AdvertId, true);

}

Related Posts:

  1. Get all posts in RSS
  2. How to get SimplePie fetch_feed without stripping iframe code?
  3. Remove or Edit in Feeds
  4. Cannot get add_feed to work
  5. Should I provide RSS or Atom feeds?
  6. Why does Simplepie return feed items in a wrong order?
  7. How to create additional full text RSS feeds
  8. Custom RSS feed forces download
  9. Multiple category RSS feeds
  10. How to flush feed? Or set timeout on feed so that it isn’t cached?
  11. How to get the last updated date of a post from a WP RSS feed?
  12. How to remove published date from RSS feed
  13. Disable only the main feed?
  14. Change the link URL in default RSS feeds
  15. WordPress Add [cdata] to title tag in rss feed
  16. A function to fetch blog content via rss feed
  17. placement of wp_error with fetch_feed
  18. RSS feed site image custom resolution
  19. How to add featured image or custom field to xml feed?
  20. Difference between the 4 Built in RSS Feeds?
  21. How to get more than 25 items via Simplepie RSS Feeds?
  22. How do I Import an RSS feed as WordPress posts without duplicates?
  23. RSS Feed Behaviour (Inc. Feedburner)
  24. How can I minimize the content of my RSS feed to fit more items in Feedburner’s 512k limit?
  25. Two RSS feeds, different items limit?
  26. How to Create RSS Feeds for categories in WordPress?
  27. Importing multiple RSS feed into WordPress and sorting them by date
  28. Delaying One RSS Feed in WordPress but Not the Others?
  29. Custom RSS does not work for categories and tags
  30. how improve website mainpage loading
  31. Add ![CDATA] to permalink rss
  32. How fetch_feed() works?
  33. Various WordPress Feeds for Same Blog
  34. How to set the cache for the built-in SimplePie feed parser?
  35. Help needed to make my site as Aggregator
  36. Custom RSS feed with custom url
  37. Showing Post ID on RSS
  38. WordPress – Sync wordpress post with JSON feed
  39. fetch_feed works on localhost but not on server
  40. WordPress appends RSS item with unwanted content
  41. Feed only showing 1 result? [closed]
  42. Where does WordPress default SimplePie save cache data?
  43. How to Fetch Rss Feeds From Other Websites
  44. Feed links not working even with add_theme_support(‘automatic-feed-links’)
  45. What’s a good RSS Importer?
  46. Is there a way to fetch RSS feed by date range rather than number of items?
  47. RSS Feed Empty in Firefox
  48. fetch_feed returning rss feeds on same server as empty
  49. Custom Feed URLs
  50. Remove ‘More..’ link from RSS feed
  51. Feed error: A feed could not be found at youtube.com/… (1 post) [closed]
  52. Getting Thumbnail in fetch_feed from custom field
  53. How do I use fetch_feed() to pull in a large number of posts?
  54. Create an RSS feed of all posts
  55. Unable to display favicon using get_favicon()?
  56. Passing feed URLs with “&” to fetch_feed()?
  57. Empty RSS Feeds
  58. Reply via email (mailto link) in RSS feed at the end of each post
  59. Second page of RSS feed
  60. RSS/Atom feeds invalid, “Mismatched tag” errors
  61. How to make an RSS feed to show most viewed posts for last 24 hours?
  62. WordPress RSS feed?
  63. fetch_feed showing only first item
  64. get_item_quantity() Error When Fetching Feed
  65. RSS feed widget for specific categories with latest posts?
  66. WordPress won’t let me use a page slug of ‘feed’
  67. WordPress keeps truncating my feeds and I can’t stop it
  68. Showing RSS feed via fetch_feed. How to show the site title of the feed?
  69. Style wordpress image captions in RSS
  70. My WordPress installation isn’t updating the feed when a new post is published
  71. RSS feed error stopping parsing of page template
  72. Use Feedburner instead of default feed on WordPress.com?
  73. What is the difference between these GUID’s in wordpress feed?
  74. Change pubDate in rss feed in another format
  75. How to remove private posts from RSS feeds?
  76. RSS – This XML file does not appear to have any style information associated with it. The document tree is shown below
  77. RSS feed has stopped updating
  78. Fetch feed from multiple sources
  79. Theme’s functions.php not called when rendering RSS feed
  80. Modify RSS feed to change iframe structure
  81. RSS Feed cURL error 52?
  82. Adding a second RSS feed
  83. How to import and parse RSS feed from a WP site to a non-WP if ‘allow_url_include’ is set to ‘off’?
  84. Error on Rss Feed for my blog
  85. RSS Feed Broken – Limit?
  86. wp_head hook content showing up at top of RSS feed
  87. Show posts without content in RSS 2 FEED
  88. are there any downsides to increasing the number of most recent items in a feed
  89. RSS2 Feed with two CDATA sections
  90. How to make my feed tidy
  91. RSS feed not displaying excerpt text?
  92. Create a “feed” from one WordPress site to another?
  93. RSS Feed Custom Title? (code placing) [duplicate]
  94. RSS feed – get specific item from external feed
  95. Embedded Feed Update
  96. WP app getting confused about posts and pages
  97. How can I define the RSS feed content length?
  98. How to add the Author on my RSS Feed
  99. RSS Subscriber count
  100. images are broken
Categories rss Tags feed, post-meta, rss, wp-insert-post
Allow user to change homepage
Custom author search

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress