I'm implementing an inventory updater on a Wordpress product site that integrates with a 3rd-party website via their custom api. We're going to have a number of people updating the 3rd-party website, and I need to set up automatic updating on our product site so that changes made to the inventory on the 3rd-party website are reflected on our product site.
I mostly have everything working like a charm (cron job and such), but I'm having some difficulty with updating the post_title. Wordpress is sanitizing the post_title when a given post (product post type) is saved. I want to be able to update the name of a product on the 3rd-party website and then save that new value to our product site automatically. I'm using wp_update_post to accomplish this:
$updatedPostId= wp_update_post( array( 'ID' => $thisId, 'post_title' => $newTitle ) );
My problem comes in where I don't want to be re-saving the post_title to every single product post if I don't need to (currently, this more than doubles the runtime of my cron job). So, I'm trying to compare the $newTitle to the current title within Wordpress. I'm doing this using the php function "strcmp":
if( ($newTitle!='')&& (strcmp($newTitle, get_the_title($thisId))!=0) )
If this statement evaluates as true, then I call wp_update_post that I showed above. My problem is that the value of the product name from the 3rd-party website isn't evaluating as being equal to the Wordpress product site post_title for a number of products (have about 2000 products on the site and this check is failing for about 100 of them). I've looked at the titles themselves and they look identical in most cases:
Eg: From 3rd party: Arnold Iron Whey [2 LBS] [Cookies & Cream] From Wordpress: Arnold Iron Whey [2 LBS] [Cookies & Cream] (Note: My text editor highlights the ampersand and the space immediately after it in the 3rd-party product name but not in the one from Wordpress)
strcmp is not evaluating to zero though, so I know that there is something different between them that only the computer can understand. Too bad I can't read invisible characters.
So, I need to "sanitize" the product name from the 3rd-party website such that it'll evaluate as identical to the already sanitized Wordpress post_title when passed through strcmp. What do I need to do to accomplish this? I found one option (sanitize_text_field) that got rid of about 20 of these false positives, but I've been working at this now for about half a day, and I'm not getting any closer to an answer that works yet.
Right now, these ~100 products are having their post_titles saved over and over again with each run of my cron_job, which I'd much rather just avoid. If at all possible.