• I received an answer yesterday from MichaelH on how to query a post and search only certain keywords that I specify. Everything works great except if it doesn’t return any posts, it still echos out that it’s trying to display it:

    <?php
    // display post title for any post that has both $findtext1 and $findtext2 in the post content
    $findtext1 = 'Welcome';
    $findtext2 = 'first';
    $args=array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'orderby' => 'date',
      'order' => 'DESC',
      'showposts' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts containing "'.$findtext1.'" and "'.$findtext2.'"';
      while ($my_query->have_posts()) : $my_query->the_post();
        if ( (strpos($post->post_content, $findtext1)!== false) && (strpos($post->post_content, $findtext2)!== false)) {   ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        } //if (strpos
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    This part is what I need help on. It’s still showing this echo statement even when there is no query results.

    if( $my_query->have_posts() ) {
      echo 'List of Posts containing "'.$findtext1.'" and "'.$findtext2.'"';

    Any ideas are welcome and much appreciated!

Viewing 15 replies - 16 through 30 (of 56 total)
  • Thread Starter fusiongt

    (@fusiongt)

    btw, here is that code in action: https://manga.lyght.net/browse/air-gear.html

    It’s searching through a couple thousand entries so it takes a bit long to load

    Additional: You have quite alot going on there, so i can’t really test the code, would mean creating categories, posts, custom fields..

    Quite capable of coding without the need to test directly, it’ll just take a little longer.

    I do hope you’re not testing this on a live server…

    Ok if that works, i’ll go from there, give you small additions to try, then i catch the problems as they arise, gimme 5 mins…

    Thread Starter fusiongt

    (@fusiongt)

    t31os, thanks…

    Yes it’s on a live server but the site isn’t live yet so no worries (no visitors are seeing it).

    Oh one thing I found was a problem,

    <?php
    #include (TEMPLATEPATH . ‘/ad_top.php’); if (in_category(‘browse-series’)) {

    Can you make the if (in_category(‘browse-series’)) { not commented (and maybe you need to close it I didn’t check) because I only want all the volumes and chapters listed if you’re in that category ‘browse-series’

    Thanks!

    As far as i can tell you’re querying hundreds or thousands of posts with every page, even single view pages..

    That’s going to eat through your resources in no time..

    Can you give me an idea of how posts relate to one another..

    I set the elements on your page to use..

    overflow:visible

    ..via CSS, and i can see even on pages with less(visible) results, there’s actually loads, just hidden out of view..

    Do your posts go into multiple categories and do you want to select piles of posts with every page view?

    Right now you seem to be selecting more then may be necessary, but i’ll need to know more about the hierarchy of your posts.. and how they are relating to one another.

    Ah, i see, that’s proberly why, it was commented out in your code, i just commented out the closing brace..

    https://wordpress.pastebin.ca/1648652

    With code uncommented.

    Thread Starter fusiongt

    (@fusiongt)

    Wait it shouldn’t be like that. I’m going to revert back to the old code so you can see how it is. Please check again , https://manga.lyght.net

    Btw it might be screwy without scroll bars, that’s ok (some javascript is doing it)

    Thread Starter fusiongt

    (@fusiongt)

    Only the pages in the category “browse-series” should query the volumes and chapters. I made a mistake and said the code you gave me worked – which it did, except it took away that limiting factor. The old code had this before it queries volumes and chapters:

    <?php if (in_category(‘browse-series’)) { ?>

    … all the huge querying goes here…

    <?php } ?>

    This way it isn’t chaos hehe.

    Thread Starter fusiongt

    (@fusiongt)

    Oh I just got your latest code and it works now as before =)

    Update again, can you just make sure it still functions.
    https://wordpress.pastebin.ca/1648682

    Just refining a few bits, trimming what can be trimmed… etc.. i’ll get to the search bit soon.. ??

    EDIT: still playing with the code..
    https://wordpress.pastebin.ca/1648703

    Appreciate if you keep testing to make sure i don’t break anything.

    Thread Starter fusiongt

    (@fusiongt)

    This one works

    https://wordpress.pastebin.ca/1648700

    I didn’t try the other, I assumed the that one was a more refined one since you edited it and added it later.

    Thanks I appreciate it. Let me know if you have more code to test ??

    The code can be seen in action here https://manga.lyght.net/browse/air-gear.html along with the rest of the site.

    https://wordpress.pastebin.ca/1648710

    Still just focussing on removing un-necessary code..

    Made a few silly mistakes hence the ninja edit…

    Check the above, looks quite slim now… ?? getting there..

    Thread Starter fusiongt

    (@fusiongt)

    Works good almost, one thing happening now is the “Chapter” is showing when no chapters are queried.

    https://manga.lyght.net/browse/absolute-boyfriend.html

    Edit – holy crap it’s a lot faster! ?? ?? ??

    Edit 2 – Maybe there doesn’t need to have any search arguments because it’s running so much better already. But your call, I’ll keep adding new code to test it

    Yeah that’s ok, it’s just because i’ve remove the counter that choose when to insert the opening part of the lists..

    Once i add the search bits previously suggested that problem will disappear..

    Apart from that minor issue(which will be fixed), does the code function as it’s suppose to?

    EDIT:

    I changed the category name part of the query “post-category” isn’t valid as far as i know, so i switched that for “category_name” which is…

    I can see there’s a problem with posts coming up now, so i’m just pointing that change out in advance..

    Thread Starter fusiongt

    (@fusiongt)

    Oh I don’t see the problem myself (just blinded by the speed of the load times… from 5+ seconds down to less than 1)… but yea I’ll try updated code when you’re ready

Viewing 15 replies - 16 through 30 (of 56 total)
  • The topic ‘Need help hiding text if a query has no posts’ is closed to new replies.