• 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 - 31 through 45 (of 56 total)
  • Try any link i post, i’ll keep posting a fresh version each time you confirm what works or what doesn’t..

    https://wordpress.pastebin.ca/1648730

    Have added in the search parameter to the above.. give it a shot..

    Thread Starter fusiongt

    (@fusiongt)

    t31 sorry for the late reply. It works well except it’s getting the volumes for the chapters:

    example:

    https://manga.lyght.net/browse/ai-ren.html

    https://wordpress.pastebin.ca/1648794

    Added some bits to print info.

    Only admin level users will see it.

    Proberly just need to refine the search part… see what prints out when you run the above..

    Off to bed shortly, i’ll check back on this tomorrow/later today.

    Thread Starter fusiongt

    (@fusiongt)

    Oh it’s very different:

    https://manga.lyght.net/browse/anima.html

    Hmm yea when I’m an admin I see a lot more stuff. When I’m not an admin it looks good (though it doesn’t show chapters but I think you mentioned that).

    When I’m an admin it adds all this top the top:

    +Anima Volume

    +Anima Chapter
    Query matched 0 posts.
    Volumes
    +Anima Volume 01
    +Anima Volume 02
    +Anima Volume 03
    +Anima Volume 04
    +Anima Volume 05
    +Anima Volume 06
    +Anima Volume 07
    +Anima Volume 09
    +Anima Volume 10
    +Anima Volume 08
    Problem finding posts in category -> shonen

    It’s also getting it by date probably because the order is different (changing it to ‘orderby’ => ‘title’, does the trick though)

    Edit – I’ve taken down that version just because I have to do more work on the site and I like the previous version. Here is screen shots of what that new version looked like:

    When viewing logged in as admin: https://lyght.net/temp/lyght-screenshot1.jpg

    When just regular user: https://lyght.net/temp/lyght-screenshot2.jpg

    Regular user (missing chapters for this series) https://lyght.net/temp/lyght-screenshot3.jpg

    OK well i’m thinking, you’ve got 2 queries in addition to the regular loop, but both use the same values other then some text to look for..

    In both cases, from what i see they both have the same starting text which is taken from a custom field.

    If they both start with the same text, i think this could be refined into 1 query, then do some sorting with the results to divide them up into 2 sections (chapters and volumes).

    So basically we say, find all posts that start with the custom field value, then loop over them results finding which ones are chapters, which are volumes, sort them into 2 arrays, merge the arrays then loop over them for display.

    Create a copy of the working code, so you don’t lose it.

    I’ll post something up in a little while.

    EDIT: Do other posts have this custom field? If this field only exists for the posts we want, we could just query by the meta key (custom field).

    Thread Starter fusiongt

    (@fusiongt)

    All the posts have custom fields – it’s how I originally planned to call on certain series (since some series have multiple names but the volume/chapter listings are under one name to keep it simple).

    Right now I’m using the 2nd to the newest code here:

    https://wordpress.pastebin.ca/1649425

    On the site it looks pretty good and is fast but I think I know what’s wrong. Here’s some examples:

    – Shows Volumes in Chapter area, https://manga.lyght.net/browse/ykk.html
    – Shows W Juliet and W Juliet II when it should only show W Juliet https://manga.lyght.net/browse/w-juliet.html
    – Shows Vagabond Volumes correctly and this time shows chapters like it should but in there also shows Volumes https://manga.lyght.net/browse/vagabond.html

    I think the “Series Volume” and “Series Chapter” isn’t being queried correctly perhaps? Or it’s searching for “Series” and ” Volume” but not both of them in the same string (which I’d like to have it accurate, this way W Juliet and W Juliet II are considered different and it’ll find W Juliet Volume and W Juliet II Volume in their respective pages).

    Can you try this version out, simply to what happens, as apart from small pieces of code, it’s untested.

    https://wordpress.pastebin.ca/1649465

    Regarding the custom field, if we were to query for posts on that key, are you saying that all posts have that key, even non-volume, and non-chapter posts (that are also in the same category).

    You know (if you don’t mind), it might help if i can see the posts, categories and related fields first-hand.. Would you mind giving me a temporary login so i can take a look?

    EDIT: Spotted a minor error.
    Use this version please – https://wordpress.pastebin.ca/1649471

    Thread Starter fusiongt

    (@fusiongt)

    t31 yea I’ll give you access and stuff but can’t figure out how to message you directly (not sure if these forums allow it)

    I tried your code and it gave a parse error:

    Parse error: syntax error, unexpected T_ENDWHILE in /home/lyghtne/public_html/manga/wp-content/themes/mangalyght/single.php on line 150

    In the code line 150 is: endwhile;

    Do you have a chat or anything I can contact you in? Google Talk – Skype – Aim? Let me know and I’ll give you access to the wp admin so you can see the categories, etc.

    EDIT – I tried your latest in your edit and it had the same line 150 parse error

    EDIT – My google talk chat is [email protected] if you’d like to contact me

    Spotted the error while i was working on it..
    https://wordpress.pastebin.ca/1649519

    Try now..

    Don’t use instant messaging apps, but i do have email.

    wp_t31osATt31osDOTcoDOTuk

    Thread Starter fusiongt

    (@fusiongt)

    Ah, got a parse error

    Parse error: syntax error, unexpected ‘}’ in /home/lyghtne/public_html/manga/wp-content/themes/mangalyght/single.php on line 149

    Hey I’m going to send you an email so we can talk through there instead

    lol, scratching my head wondering what the issue was for a while..

    Had missed a ; at the end of a line, try this..
    https://wordpress.pastebin.ca/1649532

    Thread Starter fusiongt

    (@fusiongt)

    Oh it’s getting better

    https://manga.lyght.net/browse/berserk.html

    I just sent the email let’s talk through there

    EDIT – Somethings I notice are the right column is gone and part of the post is showing (with the big ol’ image and the Ch/Volume drop down)

    Thread Starter fusiongt

    (@fusiongt)

    I also noticed the Volumes and Chapters aren’t going to their respective pages

    Fixed that, here.
    https://wordpress.pastebin.ca/1649828
    ———
    I saw the screenies.

    If posts exist with the same meta key AND/OR meta value, we can query for that without the need to use a search parameter.
    https://codex.www.ads-software.com/Template_Tags/query_posts#Custom_Field_Parameters

    If you’re using custom fields as relational data you might find adding meta(custom field) sorting to your WordPress admin handy.

    I discussed doing just that here, if it’s of interest.
    https://www.ads-software.com/support/topic/322947?replies=19

    There’s also the option of using custom taxonomies, justin did a good write-up of that here.
    https://justintadlock.com/archives/2009/05/06/custom-taxonomies-in-wordpress-28

    Not sure what you mean by right column, there’s no code for a right column in the code you’ve provided, but i do see an error coming from your header.php on alot of pages, so i can’t see any content for those.

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