Support » Fixing WordPress » check if array of post ids are in_category

  • i am checking if a post title exists already with

    if ($posts = $wpdb->get_results("SELECT ID FROM wp_posts WHERE post_title = '".mysql_real_escape_string($ap_cron_title)."' && post_status = 'publish'", 'ARRAY_A')) {

    which returns an array now I need to check that array to see if any of those posts exist in a category. If any of the post are in the category it does nothing but if it doesn’t exist in the category a post is added.

    I add posts from multiple vendors and they sometimes have the same titles for products so I check to see if that product name already exists then check to see if it is in the current vendors category. If it is not the post gets added to that category. I’ve tried a foreach for each post_ID and check if they are in_category but what happens with this is if the title is used by two or more vendors and i run the check for in_category and one show that it is not in category and the other is in category but in the foreach it adds the post for the on that doesnt match the category creating duplicate entrys so I need to somehow check the array of all post_ID’s that are generated after the title check against the category of the new post.

Viewing 1 replies (of 1 total)
  • Maybe the function wp_get_post_terms (or wp_get_object_terms) can help you. You can use it to return all the categories your post belongs to.

Viewing 1 replies (of 1 total)
  • The topic ‘check if array of post ids are in_category’ is closed to new replies.