• 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 - 1 through 15 (of 56 total)
  • My fault, should have guessed you’d end up checking for strings that didn’t exist.

    <?php
    // display post title for any post that has both $findtext1 and $findtext2 in the post content
    $count=0;
    $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() ) {
      while ($my_query->have_posts()) : $my_query->the_post();
        if ( (strpos($post->post_content, $findtext1)!== false) && (strpos($post->post_content, $findtext2)!== false)) {
          $count++;
          if ($count ==1) {
            echo 'List of Posts containing "'.$findtext1.'" and "'.$findtext2.'"';
          } ?>
          <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().
    ?>

    You could also use the search parameter instead for grabbing the required posts.

    Query posts will then only return the posts that actually contain the necessary words, rather then not showing the non-matching posts but still querying them.

    You can actually take control of the matching parameters for the search by setting the exact parameter to true.. As long as you don’t use it on an actual search you can tweak the search string..

    Consider the following..

    $args = array(
    's' => '%word1%+%word2%',
    'exact' => true
    );

    Again as long as you have no intention of printing the search query for this usage it would operate fine. Normally you’d not really use the search parameter in that way.

    When exact is set to true, the % are removed from around the search term(s). This then means you’re free to plonk your own stuff into your own search term, as above.

    Stumbled on the exact setting while i was looking at the query.php

    Sorry if i’ve derailed/hijacked at all, thought i’d share my new found knowledge…

    Darn good new found knowledge. I’ll have to try that out…Thanks t.

    Looks like a nice easy way to manipulate a search type query.

    I was hoping for something easier, and of course it ruins any need you might have to display the search string, but it does give you control over the matching parameters of the LIKE clause in the query..

    I’m not sure “how well” it works, just have a play around.. ??

    Thread Starter fusiongt

    (@fusiongt)

    Thanks Michael and t31

    Micahel, I tried to use the new search $args that T provided with the existing code you gave me, but I couldn’t get anything to show up.

    Can you please revise your code so instead of searching all the queries it just tries to return the search results? I could spend all day on it and not figure it out myself ?? I think this would also improve the speed a lot. Thanks again!

    Then go back to this example https://www.ads-software.com/support/topic/326105?replies=6#post-1263122

    Remember you have to set $findtext1 and $findtext2 to the values you are looking for.

    Also might look at these for help:
    https://www.ads-software.com/extend/plugins/tags/search

    Thread Starter fusiongt

    (@fusiongt)

    Sorry I mean is there a way to throw in the search args into your code Michael? I tried this but it didn’t work

    <?php
    // display post title for any post that has both $findtext1 and $findtext2 in the post content
    $count=0;
    $findtext1 = 'Welcome';
    $findtext2 = 'first';
    $args = array(
    's' => '%Something%+%Here%',
    'exact' => true
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();
        if ( (strpos($post->post_content, $findtext1)!== false) && (strpos($post->post_content, $findtext2)!== false)) {
          $count++;
          if ($count ==1) {
            echo 'List of Posts containing "'.$findtext1.'" and "'.$findtext2.'"';
          } ?>
          <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().
    ?>

    Of course the words Something Here were actually words I knew should return results but I couldn’t get anything to show up. Any help would be appreciated…

    Thread Starter fusiongt

    (@fusiongt)

    MichaelH, maybe I could contact you directly via email or chat (Google Talk or Skype?) to get some help on this. I could reimburse you for your time

    Where are you placing this code, i’ll be on the forum for a few hours, if you want some help via the forum i’ll happily help.

    Try broadening the search parameter just to test.

    $args = array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'orderby' => 'date',
      'order' => 'DESC',
      'showposts' => -1,
      'caller_get_posts'=> 1,
      's' => '%a%',
      'exact' => true
    );

    If that’s ok, then move onto something more complex..

    's' => %yourword%,

    Then if that’s still good, try adding another word..

    's' => %firstword%+%secondword%,

    It’s important both words exist if you use a 2 word combo..

    If you want to check for twos, but both are not required, then drop the exact parameter and place both in the search parameter like so..

    's' => firstword+secondword,

    Thread Starter fusiongt

    (@fusiongt)

    Thanks for the help… I can’t seem to get anything search related to show up. I put in the args and nothing shows up

    <?php
    // display post title for any post that has both $findtext1 and $findtext2 in the post content
    $count=0;
    $findtext1 = 'Welcome';
    $findtext2 = 'first';
    $args = array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'orderby' => 'date',
      'order' => 'DESC',
      'showposts' => -1,
      'caller_get_posts'=> 1,
      's' => '%a%',
      'exact' => true
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();
        if ( (strpos($post->post_content, $findtext1)!== false) && (strpos($post->post_content, $findtext2)!== false)) {
          $count++;
          if ($count ==1) {
            echo 'List of Posts containing "'.$findtext1.'" and "'.$findtext2.'"';
          } ?>
          <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().
    ?>

    I just kept the search query simple like you said ‘s’ => ‘%a%’, and I have a lot of posts that start with a or contain a, so something should have been returned.

    By the way, I’m showing this in single.php

    In single.php I call on this code only when:

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

    So when there’s a post in browse-series, it will then try to fetch the specific keywords. The existing code on my site (MichaelH’s working code without the search string) gets the keywords from a custom field in the post which I get like this:

    $seriestitle = get_post_meta($post->ID, $title, true);
    	$findtext1 = $seriestitle;

    Any ideas? Right now with the existing working code it’s taking a very long time trying to sort through the query (it takes 4-8 seconds to get what I want even with DB Cache activated). If I could sort it with search like you suggested I feel it’ll really improve the speed.

    Can you post the complete code from that file, so i can take a copy and test it?

    https://wordpress.pastebin.ca/

    Thread Starter fusiongt

    (@fusiongt)

    t31 sure, here you go: https://wordpress.pastebin.ca/1648596

    thanks

    Thread Starter fusiongt

    (@fusiongt)

    It’s a bit modified from the MichaelH’s suggestion because I realized I didn’t need 2 different variables (so instead of searching through all “Series” and “Volume” I’m just searching through “Series Volume” which is more refined for me)

    I think there might have been a few errors, can you just test this for me please.

    https://wordpress.pastebin.ca/1648633

    Havn’t fiddled with the queries at all, just moved code around so i can see what’s going on. I think there were a couple of minor errors that could of had an effect on the functionality. I’ve hopefully fixed that now, so if you could just confirm if the code above functions i’ll move along and add in the search parameters.

    Thread Starter fusiongt

    (@fusiongt)

    t31, yea that works

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