WordPress.org

Forums

[resolved] Looks like this if condition is not working (3 posts)

  1. Nasir Zia
    Blocked
    Posted 3 years ago #

    Hi,
    I want a query to be executed IF the following condition is true...
    but looks like the query is executed anyways.

    if($post->post_parent = '302'){
    
    // Query goes here
    }

    Is this query fine?? I want to executed the query if the parent page of current page is 302.

    Thanks

  2. michael.mariart
    Member
    Posted 3 years ago #

    You do have a problem with that if() statement, but don't worry - it's probably the single most common mistake ever made with them. :)

    What you need to is have it like this:

    if($post->post_parent == '302'){

    It looks the same, but notice that there's two ='s there, not one.

    When you have a single = there it assigns the value of $post->post_parent to 302, and returns true because it works. When you use the == (doulbe equals) it's a conditional check to see if the two are the same value.

  3. Nasir Zia
    Blocked
    Posted 3 years ago #

    Wow man.. never expected that mistake from myself :)

Topic Closed

This topic has been closed to new replies.

About this Topic