0
votes

I'm working on a wordpress website using this search facility:

http://spruce.it/noise/two-category-dropdown-search-filter-in-wordpress/

All is working OK but in the last few lines I want to include a line to say that if there are no posts in the two categories, e.g. dentists in brighton to show a 'there are no posts' kind of message. My php is very limited and what I've tried doesn't work. I was hoping to add it in before the else statement but this may not be correct. Any pointers much appreciated!

<?php else:
    if ($_GET['category'] != -1){
      header('Location:'.get_category_link($_GET['category']));
    } elseif ($_GET['city'] != -1){
      header('Location:'.get_category_link($_GET['city']));
    } else{
      header('Location: http://www.test.com');
    }
endif;?>

ADDITIONAL EDIT: here is the whole page content 14/01/14:

 <?php
require_once('../../../wp-blog-header.php');

if (($_GET['category'] != -1) && ($_GET['city'] != -1)):
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 
array( 
  'orderby' => 'date', 
  'order' => 'DESC',
  'paged' => $paged,
  'posts_per_page' => 6,
  'category__and' => array(
     $_GET['city'],
     $_GET['category']
  ),   
) 
);
?>




<?php get_header(); ?>
<div id="main">   
<div id="content" class="wide">
        <?php if (have_posts()) : ?>

       <h1><?php echo get_cat_name($_GET['category']) ?>, 
<?php echo     get_cat_name($_GET['city']) ?></h1>



         <?php remove_filter ('the_content1', 'wpautop'); ?>
         <h2>These <?php echo get_cat_name($_GET['category']) ?> can help you. They       service businesses in <?php echo get_cat_name($_GET['city']) ?><?php if(   is_category('exeter') ) echo get_page_content(585);  
        elseif( is_category('birmingham') ) echo get_page_content(587); 

elseif( is_category('') ) echo '.';?> 
       </h2>


             <ul id="search1"><?php while (have_posts()) : the_post(); ?  >


               <a href="http://www.<?php echo get_post_meta($post->ID, 'URL', true); ?>"   title="Go to the URL:  www.<?php echo get_post_meta($post->ID, 'URL', true); ?>" target="_blank">
               <li id="<?php echo get_post_meta($post->ID, 'AdSize', true); ?>"><h5><?php the_title(); ?></h5>
                <?php the_post_thumbnail( $size, $attr ); ?> 
            <div class="doubler"><?php the_content(''); ?>
                <p>Tel: <?php echo get_post_meta($post->ID, 'Telephone', true); ?><br />Website: www.<?php echo get_post_meta($post->ID, 'URL', true); ?></p></div>
                 </li>
                 </a>




                <?php endwhile; ?></ul>  


                <?php endif; ?>  






<div class="localhelp">
<h1>Find Local Help</h1>

<form method="get" id="sponsorsearchform" action="<?php bloginfo('template_url');? >/findsponsor.php" >       
 <?php wp_dropdown_categories('show_option_none=Select a Category&hide_empty=1&child_of=14&orderby=name&name=category'); ?>
<?php wp_dropdown_categories('show_option_none=Select a Location&hide_empty=1&child_of=13&orderby=name&name=city'); ?>
<input type="submit" value="Search"/>
</form>

</div>        

   <hr />   

<?php comments_template(); ?>


    </div><!--end content-->

</div><!--end main-->
<?php get_footer(); ?>     




<?php else:
if ($_GET['category'] != -1){
$currentCat = get_category($_GET['category']);
if ($currentCat->category_count > 0) {
    header('Location:'.get_category_link($_GET['category']));
} else {
    echo "No result found!";
}
} elseif ($_GET['city'] != -1){
$currentCity = get_category($_GET['city']);
if ($currentCity->category_count > 0) {
    header('Location:'.get_category_link($_GET['city']));
} else {
    echo "No result found!";
}
} else{
 header('Location: http://www.test.com');
}
endif;?>

when there are no posts in either category the findsponsor.php is shown (i.e. the page typed above) .. I want it to say no posts found but it doesn't seem to pick up the echo "No result found!", it's just the page but with no results on.

2

2 Answers

1
votes

You can get post count by using category_count attribute through get_category like;

<?php else:
if ($_GET['category'] != -1){
    $currentCat = get_category($_GET['category']);
    if ($currentCat->category_count > 0) {
        header('Location:'.get_category_link($_GET['category']));
    } else {
        echo "No result found!";
    }
} elseif ($_GET['city'] != -1){
    $currentCity = get_category($_GET['city']);
    if ($currentCity->category_count > 0) {
        header('Location:'.get_category_link($_GET['city']));
    } else {
        echo "No result found!";
    }
} else{
  header('Location: http://www.test.com');
}
endif;?>
0
votes

Your code only checks if a category or city ID is found. It does not check if there are any posts in one of those categories. You'd need to add some more code to check for that (count posts in that particular category).

Or if it is enough for you to test if a 'valid' category/city id is found, just alter the line in the else statement (redirect to home) and redirect it to a 'no posts found'-page, or echo a message there, something like that.

What is the best choice actually depends on where and when that code is executed. A little bit more explanation of the request flow is needed here.