Wordpress search not able to find the category - php

I have 3 to 4 categories on my website. I am using WordPress search but when I am searching my category then I am getting
Nothing Found
also, I tried to search the subcategory then also I am getting the same message.
Would you help me out with this issue?
search.php
<section id="primary" class="content-area mainSearch">
<main id="main" class="site-main">
<div class="equalPadding">
<div class="cp-seeWrapper">
<?php if ( have_posts() ) :
get_search_form();//search form
?>
<div class="resultCount"><?php
global $wp_query;
echo $wp_query->found_posts.' RESULTS';
?></div>
<div class="row">
<?php
while (have_posts()){the_post();?>
//displaying result here
<?php } ?>
<?php
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</div>
</div>
</div>
</main><!-- #main -->
</section><!-- #primary -->

WordPress does not include category names to search results by default. You should do this by yourself. Previously discussed here.

You can not search the category You can only search the content of the category...

Related

Blog Main Header Showing Recent Post Title Instead

My Wordpress blog works fine on the main page that shows all posts, however, when I click a category the most recent post's title becomes the main header and that post doesn't even show up in the blog post list of that category.
https://ibb.co/ebReRV
I spied on other people with the same theme as me and they have the same problem so I believe this is a problem with the original code. My theme's creator seems to have disappeared and hasn't responded to any of my messages.
archive.php
<section id="wp-main-content" class="clearfix main-page title-layout-standard">
<?php do_action( 'naturalfood_before_page_content' ); ?>
<div class="container">
<div class="main-page-content row">
<!-- Main content -->
<div class="content-page <?php echo esc_attr($main_content_config['class']); ?>">
<div id="wp-content" class="wp-content">
<?php get_template_part('templates/layout/archive') ?>
</div>
</div>
You Need to Create Archive Page
Give Archive Page name is "archive.php".
Archive Page Structure Like this:
<?php
/*
Template Name: Archives
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
/* Custom Archives Functions Go Below this line */
/* Custom Archives Functions Go Above this line */
</div><!-- .entry-content -->
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

custom theme for wordpress new page

I created a custom theme for word-press. When i say 'add new' page and i put content into this page it only displays the footer and header but dosnt hook my content which is in the CMS on that page.
This is what i have : page.php
<?php get_header(); ?>
<div class="row">
<div class="col-sm-12">
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; endif;
?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
Its been a long day what am i doing wrong here. Any tips please?
Would also like to make like a parent page from this new page, but its only option with page attributes are 'default template'.
If you didn't have a template part please use the_content() instead of
get_template_part( 'content', get_post_format() );
My actual problem, resolved. So i used 'advanced custom field' plugin for my WordPress. The problem using this is that it still requires you to do some coding.
I added a new page.
For that page I had an header image and content. So for ACF(advanced custom field) that is equal to two field types "Image --> field type and Text-Area --> field type"
Field name is what i used to call the fields to my .php page template.
Field Name : Image = header-image
Field Name : (2)Text Area = header-image-text-top (and) header-image-text-author
(I needed to display my text on the image thats why my div is just based on header image.)
So I hooked my page template and then added the the hooks for the fields i want to display on this page template.
page-stay.php :
<?php
/*Template Name: Stay - Parent Page */
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post(); ?>
<article id="post=<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- <header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header>(you can use this if you need your page header to display)-->
<div class="enry-content">
<?php
$image = get_field('header-image');
if( !empty($image) ): ?>
<div class="head-image">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<div class="head-text-box">
<div class="head-text-box-holder">
<p class="headtext-quote"><?php the_field('header-image-text-top'); ?></p>
<p class="headtext-author"><?php the_field('header-image-text-bottom'); ?></p>
</div>
</div>
</div>
<?php endif; ?>
</article>
<?php endwhile; ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>

Customize search results by category

I have different categories where the post displayed are characterized by different elements, for this reason when I use the search in wordpress the results are not shown very well.
For this reason I would like to change the appearance of the post shown in the search results based on its category.
Example:
My search.php template look like this:
<?php get_header(); ?>
<div id="content">
<div class="padder">
<?php do_action( 'bp_before_blog_search' ); ?>
<div class="page" id="blog-search" role="main">
<?php if (have_posts()) : ?>
<?php bp_dtheme_content_nav( 'nav-above' ); ?>
<?php while (have_posts()) : the_post(); ?>
<?php do_action( 'bp_before_blog_post' ); ?>
<div class="blog-post">
// Here is displayed the blog post style and features
</div><!--blog-post-->
<div class="clear"> </div>
<?php do_action( 'bp_after_blog_post' ); ?>
<?php endwhile; ?>
<?php bp_dtheme_content_nav( 'nav-below' ); ?>
<?php else : ?>
<h2 class="center"><?php _e( 'No posts found. Try a different search?', 'OneCommunity' ); ?></h2>
<?php endif; ?>
<div style="display:inline">
<center><?php wp_pagenavi(); ?></center>
</div>
</div>
<?php do_action( 'bp_after_blog_search' ); ?>
</div><!-- .padder -->
</div><!-- #content -->
<div id="sidebar">
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar-blog')) : ?><?php endif; ?>
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar-ad-blog')) : ?><?php endif; ?>
</div><!--sidebar ends-->
<?php get_footer(); ?>
I tried to achieve the result of displaying based on the post category a different "template" for the post but without success.
I asked the same question on wordpress.stackexchange.com but without success, and there I pointed out a non working solution, hope it can help you in developing the code I need.
https://wordpress.stackexchange.com/questions/141856/different-layout-on-search-page-depending-on-category-post
Thank you very much for any help you will give, and excuse me for my english.
If I understand your question correctly, you could simply use the post_class() function:
<div <?php post_class( 'blog-post' );?> >
// Here is displayed the blog post style and features
</div><!--blog-post-->
This will render like:
<div class="post-3654 post type-post status-publish format-standard hentry category-buildings blog-post">
// Here is displayed the blog post style and features
</div><!--blog-post-->
for posts in the buildings category.
Then your CSS will take care of the rest:
.post.category-buildings {
border: 3px solid red;
}
But I think your setup/idea is only suitable for single category posts.
Ref: From the Codex:
The post_class may include one or more of the following values for the
class attribute, dependent upon the pageview.
.post-id
.post
.attachment
.sticky
.hentry (hAtom microformat pages)
.category-ID
.category-name
.tag-name
.format-name

How to display posts with a certain tag

I'm really struggling at what I am sure is a simple problem. I cannot seem to get the posts which fall under a certain tag to display on that tags page ie: (/tag/blog/) with blog being the Tag.
So far following the Official Tag Templates page on Wordpress I still cannot seem to get it working.
I do not need a heirarchy so tag.php works fine. Using single_tag_title() it does correctly display the tag at the top of the page.
The rest of the Official Tag Templates does not really give much more detail on wether or not I can use the default Loop or a custom one. I have tried with the custom one as shown below but that does not work. (I've kept it down to the minimum not currently worried about styling.)
<?php get_header(); ?>
<p>Tag: <?php single_tag_title(); ?></p>
<div class="container">
<div id="content" class="clearfix row">
<div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main">
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_title();
endwhile; // end while
endif; // end if
?>
</div> <!-- end #main -->
<?php get_sidebar('blog'); // Blog ?>
</div> <!-- end #content -->
</div>
<?php get_footer(); ?>
So this code currently does display the title of the Tag but not the title of the posts which I am trying to get out.
To wrap the question up. "How do I display the posts which fall under a certain tag."
You're not telling the loop to look for that particular tag anywhere...all you're doing is displaying the current selected tag at the top of the page. Add a PHP if statement into your loop to grab the posts with that tag:
<?php get_header(); ?> // Get the header
<p>Tag: <?php single_tag_title(); ?></p> // Display the tag name
<div class="container">
<div id="content" class="clearfix row">
<div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> // Start your loop
<?php if(tag_slug( ' THE SLUG ' ) ) : ?> // Look for the tag
// Post HTML stuff
<?php endif; ?>
<?php endwhile; endif ?> // finish the loop
</div> <!-- end #main -->
<?php get_sidebar('blog'); // Blog ?>
</div> <!-- end #content -->
</div>
<?php get_footer(); ?>
This page from the Codex has examples of how to grab categories and tags.
You just need the the_post() function, you have to call it inside the while, and it should be the first function you call, as it loads the template tags.
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
the_title();
endwhile; // end while
endif; // end if
?>
if the tags is just used in custom post types you should add something like this to you functions.php:
add_action( 'pre_get_posts', 'custom_function' );
function custom_function($query){
if(! is_admin()){
if(is_tag()){
$query->set('post_type', array('your_post_type', 'your_other_post_type', 'post'));
}
}
}
I found that I had to use a WP Query loop to bring them in due to being custom post types.
<?php if ( is_tag() ) {$term_id = get_query_var('tag_id'); $taxonomy = 'post_tag'; $args ='include=' . $term_id; $terms = get_terms( $taxonomy, $args );} ?>
<!-- This gets the tags slug -->
<?php $query = new WP_Query(array( "post_type" => array('blog', 'portfolio'), "tag" => $terms[0]->slug ) ); while ($query->have_posts()) : $query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>

Custom Post Meta Search Template

I'm struggling with getting wordpress to search custom post meta from a page template. I've looked all over the internet and can't seem to find anything that will work. No plugins seem to work either.
On my posts, I have the custom meta: "rate" and the value: "10" - Wordpress delivers no result when searching any of these.
I'd be very appreciated if someone could write me a searchpage.php page template or point me in the right direction (I'm not good with php).
Here's my current PHP code:
<?php
/*
Template Name: Search Custom Meta
*/
?>
<?php get_header(); ?>
<div id="content">
<div class="container clearfix fullwidth">
<div id="left-area">
<?php query_posts('meta_value='.$s); ?>
<?php if (!empty($wp_query->posts)) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
</div> <!-- end #left-area -->
</div> <!-- .container -->
</div> <!-- #content -->
<?php get_footer(); ?>
You checking incorrect variable in if so try removing that one,
query_posts('meta_value='.$s);
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;

Categories