search result with search term with category in wordpress
<?php
/**
* The template for displaying Search Results pages.
*/
get_header();
?>
<?php
$s = $_GET['s'];
$post_type = $_GET['post_type'];
$cat_terms = $_GET['cat_terms'];
$category_ID = get_category_id($cat_terms);
?>
<?php
$search_args = array( 'post_type' => 'listing', 'orderby' => 'post_date', 's' => $_GET['s'], 'posts_per_page' => -1, 'order' => 'DESC', 'post_status' => 'publish');
$cat_args= array(
'post_type' => 'listing',
'order' => 'DESC',
'orderby' => 'post_date',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'education_type',
'field' => 'term_id',
'terms' => $category_ID
),
),
);
if(isset($cat_terms))
{
$final_posts = array_merge( $search_args, $cat_args );
$loop = new WP_Query( $final_posts );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<br>';
endwhile;
}
else
{
$loop = new WP_Query( $search_args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<br>';
endwhile;
}
?>
<?php get_footer(); ?>
/**
* The template for displaying Search Results pages.
*/
get_header();
?>
<?php
$s = $_GET['s'];
$post_type = $_GET['post_type'];
$cat_terms = $_GET['cat_terms'];
$category_ID = get_category_id($cat_terms);
?>
<?php
$search_args = array( 'post_type' => 'listing', 'orderby' => 'post_date', 's' => $_GET['s'], 'posts_per_page' => -1, 'order' => 'DESC', 'post_status' => 'publish');
$cat_args= array(
'post_type' => 'listing',
'order' => 'DESC',
'orderby' => 'post_date',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'education_type',
'field' => 'term_id',
'terms' => $category_ID
),
),
);
if(isset($cat_terms))
{
$final_posts = array_merge( $search_args, $cat_args );
$loop = new WP_Query( $final_posts );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<br>';
endwhile;
}
else
{
$loop = new WP_Query( $search_args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<br>';
endwhile;
}
?>
<?php get_footer(); ?>
Comments
Post a Comment