WordPress.org

Support

Support » How-To and Troubleshooting » get post ID using meta-key query

get post ID using meta-key query

  • I just want to get post ID whose sku is GS-xxxxx .
    If no post exists, i’l create a new one.

    I thought this was going to be a snap.
    I need to get a post’s ID based on query for a particular SKU.
    custom field= “sku”
    the meta-value shown here exists.
    I can get the ID, but how do I check if there was NO ID?

    // retrieve one post with determined sku
    query_posts('meta_key=sku&meta_value=GS_5-00252');
    while (have_posts()) : the_post();
    $str = get_the_ID() ;
    echo $str;
    $len= strlen($str);
    if (1 > $len ) {
    echo "NO POST yet, create';
    } else {
    echo $len;
    }
    endwhile;

    I’m OK till I hit the IF statement.
    I need to check if the ID exists, if not, then I will create a new post.
    for the life of me, can’t figure out what’s wrong with that if statement.
    I thought maybe missing a “;” after the }… but not the case (I think).
    stumped… can anyone save me some headaches playing with this?

Viewing 2 replies - 1 through 2 (of 2 total)
  • hey, no way to delete a post?

    this will remain as testimony to my blindness?

    problem solved.
    echo “NO POST yet, create’;

    mismatched quote and single quote mark.

    doh !works now.

    must get new glasses.

    Since the post is here, you might as well get the correct way of doing it 🙂

    query_posts('meta_key=sky&meta_value=GS_5-00252');
    if (have_posts()) :
        while (have_posts()) : the_post();
            $str = get_the_ID() ;
            echo $str;
        endwhile;
    else :
        echo 'NO POST yet, create';
    endif;
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get post ID using meta-key query’ is closed to new replies.
Skip to toolbar