WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. vincej
    Member
    Posted 3 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
    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?

  2. vincej
    Member
    Posted 3 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
    Member
    Posted 3 years ago #

    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;

Topic Closed

This topic has been closed to new replies.

About this Topic