Support

Account

Home Forums General Issues Issue with post object and if/else

Solved

Issue with post object and if/else

  • I’m having trouble with a post object field in an if/else scenario.

    <?php if( get_field('obits-second_visitation_location') != 'Announcement Pending' ): ?>
    <?php
        $post_object = get_field('obits-second_visitation_location');
            if( $post_object ):
            // override $post
            $post = $post_object;
            setup_postdata( $post );
            // variables and such (working)
            $locAdd1 = get_post_meta($post->ID, 'location-address', 1);
    ?>
    <strong><?php the_title(); ?></strong> (along with the variables and other stuff; working)
    
    <?php wp_reset_postdata(); ?>
    // endif for $post_object
    <?php endif; ?>                    
    <?php else: ?>
        <em>Location TBA</em>
    
    // endif for original if
    <?php endif; ?>                       
    <?php endif; ?>

    So that’s what I’ve got. I’ve also tried essentially the opposite (swapping the if and else around and using == instead of !=) but that wasn’t working right either. I’m wondering if the “answer” to my “if” question needs to be something besides the title of the post (perhaps a post ID or something), but I’m not sure how I should format that if that is the case. It does seem that the query isn’t recognizing “Announcement Pending” as a cue and is essentially performing the else function regardless of actual selection.

    I should mention I’m a Google educated PHP noob and I feel like most of this particular project has been over my head, but up until this point I’ve managed to squeak by and keep things functional. This particular riddles seems to have me stumped, though.

  • I realize my wording was a tad ambiguous there in that second to last paragraph. I didn’t switch the actual words if and else, just what’s supposed to happen under each condition (e.g. if field == 'Announcement Pending' show <p>TBA</p>, else that whole mess of stuff).

  • One thing you can do is try to debug on what the actual returned value is.

    Try to do dump out the value before your if statement, and see if it’s actually ‘Announcement Pending’.

    Something like:

    
    var_dump(get_field('obits-second_visitation_location'));
    
    if (....
    ...
    

    Cheers.

  • Thanks, @gummiforweb; here’s the var_dump:
    object(WP_Post)#3438 (24) { ["ID"]=> int(96) ["post_author"]=> string(1) "4" ["post_date"]=> string(19) "2017-10-18 19:44:55" ["post_date_gmt"]=> string(19) "2017-10-18 19:44:55" ["post_content"]=> string(0) "" ["post_title"]=> string(20) "Announcement Pending" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(10) "pendingtba" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2017-10-18 19:45:44" ["post_modified_gmt"]=> string(19) "2017-10-18 19:45:44" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(58) "http://mytestingdomain.com/?post_type=location&p=96" ["menu_order"]=> int(0) ["post_type"]=> string(8) "location" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

    I see lots of stuff and honestly, I don’t really know what I’m looking for, but perhaps it’s no coincidence that the first thing in the list is the post number and id? That said, I do see the post title is showing correctly, so maybe instead of get_field I should try something that check’s the post title or ID?

    Actually that doesn’t make sense either, since I’m trying to use the post_object field on this post to figure out which related location post to pull info from. In other words, I can’t grab the post_title until I know which title I’m supposed to be looking for. Unless I’m missing something…Like I said before, I’m still pretty green with these PHP thingies so I certainly appreciate any help sorting through my mess here!

  • So, your field’s return type is set to object, that’s is why your condition is always false. Because an object will not equal to a string.

    If you wanna check the condition by the post title, you try something like:

    
    $post_object = get_field('obits-second_visitation_location');
    
    if (! $post_object || $post_object->post_title != 'Announcement Pending'):
        ......
    

    Cheers.

  • Alright now we’re talking…thanks again, @gummiforweb! That solved my issue 🙂

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.