Dhawal Mhatre
Dhawal Mhatre

Reputation: 435

Advance Search not showing proper result in Wordpress?

I have added advance search in Wordpress Search.. please check below image.. enter image description here

Below is my search Code in wordpress :

 global $wp_query;
  $args = array (
    's' => $s,
    'cat' => $category,
    'year' => $year,
    'monthnum' => $monthnum
  );


$query = new WP_Query($args) ;

<?php if ( $query->have_posts() ) : ?>
    <?php while (have_posts() ) : the_post(); ?>
    <article>
    <h2><?php the_title(); ?></h2>
        <div class="entry">
        <?php the_content( __( 'Read More &raquo;', 'tie' ) ); ?>
        </div>        
    </article>
  <?php endwhile; ?>
  <?php endif; ?>

From above code i am getting proper result... but when i change the category the result stays same...??

If i get search results as 1,2,3,4,5,6,7,8,9 and on change on category i need 1,5,6,8,9

what is wrong with my code..??

Upvotes: 0

Views: 81

Answers (1)

Noman
Noman

Reputation: 4116

First check your $category is null or not. if it not showing the related to category. also make sure you $category is integer

See what WP_QUERY use category arguments.

cat (int) - use category id.
category_name (string) - use category slug.
category__and (array) - use category id.
category__in (array) - use category id.
category__not_in (array) - use category id.

Change your loop to below code.

For using pagination you may need to change your $args array with the required arguments.

// for number of page.

posts_per_page , paged used for pagination.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = 8; // records per page.

$args = array (
    's' => $s,
    'cat' => $category,
    'year' => $year,
    'monthnum' => $monthnum , 
    'posts_per_page' => $per_page,
    'post_status' => 'publish',
    'paged' => $paged,
  );



// the query
$query = new WP_Query( $args ); ?>

<?php if ( $query->have_posts() ) : ?>

    <!-- pagination here -->

    <!-- the loop -->
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- pagination here -->

    <?php if (function_exists("pagination_custom")) {
            pagination_custom($query->max_num_pages);
    } 
    ?>

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

UPDATE : For Pagination:

// Pagination code
function pagination_custom($pages = '', $range = 4)
{
    $showitems = ($range * 2) + 1;

    global $paged;
    if (empty($paged)) $paged = 1;

    if ($pages == '') {
        global $wp_query;
        $pages = $wp_query->max_num_pages;
        if (!$pages) {
            $pages = 1;
        }
    }

    if (1 != $pages) {
        echo "<div class=\"pagination\"><ul>";
        if ($paged > 2 && $paged > $range + 1 && $showitems < $pages) echo "<a href='" . get_pagenum_link(1) . "'>&laquo; First</a>";
        if ($paged > 1 && $showitems < $pages) echo "<a href='" . get_pagenum_link($paged - 1) . "'>&lsaquo; Previous</a>";

        for ($i = 1; $i <= $pages; $i++) {
            if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) {
                echo ($paged == $i) ? "<li><a href='" . get_pagenum_link($i) . "' class=\"current\">" . $i . "</a></li>" : "<li><a href='" . get_pagenum_link($i) . "' class=\"inactive\">" . $i . "</a></li>";
            }
        }

        if ($paged < $pages && $showitems < $pages) echo "<a href=\"" . get_pagenum_link($paged + 1) . "\">Next &rsaquo;</a>";
        if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) echo "<a href='" . get_pagenum_link($pages) . "'>Last &raquo;</a>";
        echo "</ul></div>\n";
    }
}

Upvotes: 1

Related Questions