• So I’m trying to get 4 queries going on my index. They all work except one of them. It simply doesn’t show up even though I’ve copied all the other ones that do work and just changed the query name. I’m not getting post thumbnail or the_content or errors. Posting the code:

    $args = array(
        'post_type' => 'post',
        'orderby' => 'rand',
        'category_name' => 'venues',
        'posts_per_page' => 1,
        'paged' => $paged
    ); $venues = new WP_Query($args);

    And here’s my loop:

    <?php if ($venues->have_posts()) : while($venues->have_posts()) : $venues->the_post(); ?>
    
        <?php if(has_post_thumbnail()) { ?>
    
           <span class="item" style="background-image: url('<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>');"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">&nbsp;</a></span>
                        <?php
                            $trimtitle = get_the_title();
                            $shorttitle = wp_trim_words( $trimtitle, $num_words = 10, $more = '... ' );
                            echo '<h2><a href="' . get_permalink() . '">' . $shorttitle . '</a></h2>';
                        ?>
                        <?php $content = get_the_content(); echo '<p>' . mb_strimwidth($content, 0, 270, ' ... ') . ' <a href=" ' . get_permalink() . ' ">more</a>' . '</p>'; ?>
    
                    <?php } else echo '<span class="item" style="background-image: url(\'https://www.harshclimate.com/wp-content/uploads/2019/01/arizonaflag.jpg\');"><a href="' . get_the_permalink() . '" title="' . get_the_title() . '">' . '&nbsp;' .  '</a></span>'; ?>
    
                <?php endwhile; else : '<p>Sorry, no venue posts have been added yet.</p>'; endif; wp_reset_postdata(); ?>

    I do have a category named “Venues” and I do have posts attached to that category. Beyond all that, I have no idea why nothing under that query is showing up.

Viewing 13 replies - 1 through 13 (of 13 total)
  • Thread Starter harshclimate

    (@harshclimate)

    Thanks for the response! I don’t think it’s an issue with the query itself, though. I did just build a CPT for what I need and the post thumbnail shows up, but the_content doesn’t. I just don’t get it!

    So now the query is:

    <?php
        $args = array(
            'post_type' => 'store',
            'orderby' => 'rand',
            'posts_per_page' => 1,
            'paged' => $paged
        ); $store = new WP_Query($args);
    ?>

    And I changed the loop in the previous example accordingly

    • This reply was modified 5 years, 10 months ago by harshclimate.
    Thread Starter harshclimate

    (@harshclimate)

    Maybe it has something to do with this?

    <?php $content = get_the_content(); echo '<p>' . mb_strimwidth($content, 0, 270, ' ... ') . ' <a href=" ' . get_permalink() . ' ">more</a>' . '</p>'; ?>

    Moderator bcworkz

    (@bcworkz)

    Heya harshclimate,

    A couple things that may or may not cause problems, depending on the actual template context:

    1. There is no point in using “paged” if you are querying for 1 random post. If the main query is paged, this will definitely mess up your query.

    2. You get the post ID with $post->ID. Unless $post is declared global within scope of your template, this will fail. Within the loop on templates, it’s best to get the ID with get_the_ID(). Funny how that works ?? This could be the cause of featured image issues.

    I tested your code on my site, using “post” post type and a valid category for my site, and with the above mentioned adjustments; the content was output in all cases. There was no trouble with your mb_strimwidth() line.

    There could be an issue with content processing on your site. Take content processing out of the equation by getting the content directly from global $post instead of from get_the_content()

    global $post;
    $content = $post->post_content;
    echo '<p>' . mb_strimwidth( ...etc.

    If this change works, there’s an issue with the processing done for get_the_content().

    Thread Starter harshclimate

    (@harshclimate)

    Okay, let me give it a shot…

    Thread Starter harshclimate

    (@harshclimate)

    I removed the instance of $paged from the query. I also changed up one of the loop posts to include your rendition of the global $post example:

    <?php 
       global $post; $content = $post->post_content; echo '<p>' . mb_strimwidth($content, 0, 
       270, ' ... ') . ' <a href=" ' . get_permalink() . ' ">more</a>' . '</p>'; 
    ?>

    Is this correct? I’m not getting any errors but I’m still not getting any content other than the post_thumbnail…

    Thread Starter harshclimate

    (@harshclimate)

    Moderator bcworkz

    (@bcworkz)

    That snippet worked on my site. Of course I’ve no wineries, but it output the content I did have. None the less, let’s take the trimming out of the equation and dump out the entire post object.

    <pre><?php
      global $post;
      var_dump($post);
    ?></pre>

    You should see the entire post object, including content. If you see post data, but no content, then the content is coming from somewhere else.

    If you haven’t done so, define WP_DEBUG as true in wp-config.php until this is resolved. Address any errors that show up.

    Thread Starter harshclimate

    (@harshclimate)

    Thanks, BC: Here’s what I have from the dump…

    object(WP_Post)#713 (24) {
      ["ID"]=>
      int(212)
      ["post_author"]=>
      string(1) "1"
      ["post_date"]=>
      string(19) "2019-01-07 18:16:20"
      ["post_date_gmt"]=>
      string(19) "2019-01-08 01:16:20"
      ["post_content"]=>
      string(267) "
    This is a test store to see if I can figure out what's going on with the plaza liquors one. Very strange that the thumbnail will show up but not the content text. I don't know if I can figure this one out on my own.
    ["post_title"]=>
      string(18) "Testing This Store"
      ["post_excerpt"]=>
      string(0) ""
      ["post_status"]=>
      string(7) "publish"
      ["comment_status"]=>
      string(4) "open"
      ["ping_status"]=>
      string(4) "open"
      ["post_password"]=>
      string(0) ""
      ["post_name"]=>
      string(18) "testing-this-store"
      ["to_ping"]=>
      string(0) ""
      ["pinged"]=>
      string(0) ""
      ["post_modified"]=>
      string(19) "2019-01-07 18:16:20"
      ["post_modified_gmt"]=>
      string(19) "2019-01-08 01:16:20"
      ["post_content_filtered"]=>
      string(0) ""
      ["post_parent"]=>
      int(0)
      ["guid"]=>
      string(29) "https://www.azcork.com/?p=212"
      ["menu_order"]=>
      int(0)
      ["post_type"]=>
      string(4) "post"
      ["post_mime_type"]=>
      string(0) ""
      ["comment_count"]=>
      string(1) "0"
      ["filter"]=>
      string(3) "raw"
    }
    • This reply was modified 5 years, 10 months ago by harshclimate.
    Thread Starter harshclimate

    (@harshclimate)

    Maybe I should just redo the entire index? I’ve actually never had this happen before in any of the sites I’ve done.

    Were you able to take a look at the gist I pasted? Maybe that could give you an idea of my psychosis ??

    Moderator bcworkz

    (@bcworkz)

    I did look, and didn’t see any issues. Nothing like trying code on a site though, which I didn’t do, I only tried a small snippet of the while thing.

    I wouldn’t redo everything, you’re very close, even if the way forward is not clear. The var_dump includes post content (“This is a test store to see…”), so the data is available in the loop, it’s now just a matter of getting it properly output. A next logical progression is to replace var_dump($post); with echo esc_html($post->post_content); (and drop the pre tags). This will at least give you the complete content. Then it’s a matter of limiting output longer than a certain length.

    Not sure why mb_strimwidth() or something adjacent would cause trouble for you, it works for me. Perhaps try using wp_trim_words() instead? Keep making incremental improvements in the code, testing each small change. Then if something breaks, you’ll know exactly the cause.

    Thread Starter harshclimate

    (@harshclimate)

    Oh wow. I think I just found the issue. I looked at the code again and the title and content are between the post_thumbnail. So if there’s a post thumb the content shows up, but if there isn’t, the content won’t show! hah!

    Are you seeing what I see?

    If I’m correct, I have no clue how I ended up doing that. I’m usually so careful!

    • This reply was modified 5 years, 10 months ago by harshclimate.
    • This reply was modified 5 years, 10 months ago by harshclimate.
    Moderator bcworkz

    (@bcworkz)

    Hah! Now that you mention it it, yes. There’s the fallacy of testing only snippets, it assumes the logic is correct when it’s not.

    I think the way you structured the conditional threw us off. You have something like

    if (have_thumbnail()) {
       do_stuff();
    } else do something else;

    Valid syntax but poor style. It’s better to always use block delimiters even for single lines of code.

    if (have_thumbnail()) {
       do_stuff();
    } else {
       do something else;
    }

    I think the logic blunder would have been more obvious. Or maybe we’re just blind and there’s no explanation ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Strange issue with WP_Query’ is closed to new replies.