get post ID using meta-key query (3 posts)

  1. vincej
    Posted 5 years ago #

    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
    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;

    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?

  2. vincej
    Posted 5 years ago #

    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.

  3. Biranit
    Posted 5 years ago #

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

    if (have_posts()) :
        while (have_posts()) : the_post();
            $str = get_the_ID() ;
            echo $str;
    else :
        echo 'NO POST yet, create';

Topic Closed

This topic has been closed to new replies.

About this Topic