• Hi all,

    Here’s what I’m trying to do:

    Say I use 10 custom fields when publishing a post. All 10 are always assigned a value.

    I want to have users insert new posts with wp_insert_post().
    The user fills in all 10 custom fields and hits submit.

    For the post to get submitted, I want to check within all posts on the site IF there is any other post that contains the exact same values for those 10 custom fields.

    This is a personal project I’m attempting and it’s basically a sort of classifieds ads where I need to check if the exact same ad was already posted, so people don’t spam the site submitting the same ad multiple times.

    Hope I wasn’t too confusing ??

    Thanks,
    Cosmin.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Cosmin

    (@gizmoc)

    Nobody? ??

    So what exactly you don’t know?

    Before inserting the post, query all your posts and check if the values are the same. if they are not, insert your new post in the database, else display an error or something.

    Thread Starter Cosmin

    (@gizmoc)

    Thanks Gabriel, but I don’t really know how to check or compare the values after querying all the posts.

    A code example would help me a lot, if it’s not too much to ask.

    Thanks again! ??

    Well, I think the only option is to really query all your posts, and search for that combination, because wordpress stores custom values is postmeta table and in a serialized array format.

    Saying you have 3 custom meta defined:
    – ad_title
    – ad_url
    – ad_image

    You start by querying all the posts

    $my_posts = $wpdb->get_results("SELECT id FROM $wpdb->posts');
    // this will tell us if that data already exists
    $insert = 1;
    // now search for that posted values
    foreach($my_posts as $p) {
       if($insert == 0) break; // stop the foreach loop if we found the entry in the database
           $ad_title = get_post_meta($p->id, 'ad_title', true);
           $ad_url   = get_post_meta($p->id, 'ad_url', true);
           $ad_image = get_post_meta($p->id, 'ad_image', true);
          // saying that you post the vars that user imputs, test if we found it in the database and change $insert to 0 if we do
           if(($_POST['ad_title'] == $ad_title) && ($_POST['ad_url'] == $ad_url) && ($_POST['ad_image'] == $ad_image )) {
               $insert = 0;
           }
    }
    if($insert == 1) {
        wp_insert_post(data you have collected);
    }

    Thread Starter Cosmin

    (@gizmoc)

    Thanks Gabriel, I’ll try it tonight and let you know here ??

    Thread Starter Cosmin

    (@gizmoc)

    Man, thanks for the help!

    I ended up not using the exact code you provided, but a modified version of this codex example.

    So, for others that might have the same problem I had, here’s the full code.

    https://wordpress.pastebin.com/j1anH8Gj

    To quickly check it out, using Twenty Ten, simply save the following as page-inserter.php and create a page using it as a template.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Custom Fields] Check if any post has the exact same custom fields values’ is closed to new replies.