WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to get post id on certain meta data values (3 posts)

  1. mvtilborg
    Member
    Posted 2 years ago #

    I am new to plugin development and I am working on one that adds posts from xml data. Each item from the xml contains two unique values (affiliate id and a product id) that I store as Meta Data along with the post.

    However the feed updates once in a while, so now I need to figure a way to check if a value is present in the meta data of any of my posts. I need the id of that post in my plugin script to decide whether to create new post or update an existing post.

    How can that be achieved?

  2. mvtilborg
    Member
    Posted 2 years ago #

    Figured it out myself

    global $wpdb;
    		$querystr = "SELECT post_id, count(post_id)
    			FROM $wpdb->postmeta
    			WHERE
    				(meta_key = 'product_aid' AND meta_value = '". $aid ."') OR
    				(meta_key = 'product_id' AND meta_value = '". (string)$item->productID ."')
    			GROUP BY post_id
    			HAVING count(post_id) = 2;
    		";
    		$postid = $wpdb->get_var($wpdb->prepare($querystr));
  3. Frankie Jarrett
    Member
    Posted 2 years ago #

    Worked like a charm. Thanks for sharing this!

Topic Closed

This topic has been closed to new replies.

About this Topic