• Resolved v123shine

    (@v123shine)


    Hello,

    I have custom post type NEWS (single-news.php). In post type NEWS i created a new custom meta key uk_news_info.

    I’m using this code for display if/else conditional for custom meta key uk_news_info:

    <?php if ( get_post_meta( get_the_ID(), 'uk_news_info', true ) ) : ?>
    <!--text/code-->
    <?php endif; ?>

    Everything work fine.

    In post type POST (single.php), i want retrieve this meta key uk_news_info from post type NEWS. I’m using this code >> https://pastebin.com/3c16yFvf
    And everything work good.

    My question is, how to create if conditional for this situation https://pastebin.com/3c16yFvf in post type POST or single.php file?
    I mean, if empty meta key uk_news_info in post type NEWS with post id 123, dont show that code.

    Can someone help me, please!

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter v123shine

    (@v123shine)

    UPDATE:
    Please ignore the above question, this is the right question:

    My question is, how to create if conditional (https://pastebin.com/3c16yFvf) for this situation:
    Don’t show the code if meta key uk_news_info in post type NEWS don’t have current post ID or get_the_ID();

    Please help me.

    • This reply was modified 7 years, 6 months ago by v123shine.
    • This reply was modified 7 years, 6 months ago by v123shine.
    Moderator bcworkz

    (@bcworkz)

    Sorry, I’m still confused. You said “if meta key uk_news_info in post type NEWS don’t have current post ID”. Is that not the criteria in the query? You shouldn’t be getting posts returned where meta key uk_news_info in post type NEWS don’t have current post ID. Is that query criteria not working? But you say it is working well.

    Thread Starter v123shine

    (@v123shine)

    Dear bcworkz,

    I think i’m forget to explain, in uk_news_info i already set current post ID.

    Array
    (
        [0] => 1357
    )

    1357 is current post ID (single.php).
    Can you help me, please!

    Thanks

    Moderator bcworkz

    (@bcworkz)

    I see. You are getting posts that are related to the current post. Related by the value in uk_news_info meta key of the related posts. That is how you’ve specified the query, so there should only be posts that are related. Only posts with meta value the same as the current post ID should be returned, so there is no need for a conditional statement.

    To answer your question anyway, do this inside the while loop:

    if ( get_post_meta( get_the_ID(), 'uk_news_info', true ) == $post_id ) {
       //do something
    }
    Thread Starter v123shine

    (@v123shine)

    Dear bcworkz, thank you so much for help me.

    Solved!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘If conditional for meta key?’ is closed to new replies.