Wordpress - Display only pages with specific template in search results - php

I want to display in my WP search results ONLY pages with specific template beceuse I trying to build product catalog without any plugins. Pure WP code. Some pages are my products, and they have product.php template file. This is my search.php:
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/post/content', 'excerpt' );
endwhile;
the_posts_pagination();
else :
?>
<p><?php _e( 'No results.' ); ?></p>
<?php
get_search_form();
endif;
?>
And the question is, how to display only my product pages without any other pages?

Try this out!
This function will interrupt the search query and add some condition fo the query to returns specific pages.
//Filter the search for only posts and parts
function SearchFilter($query)
{
// Now filter the posts
if ($query->is_main_query() & $query->is_search && $search_template) {
$query->set('post_type', 'page');
$query->set('meta_key' => '_wp_page_template',);
$query->set('meta_value' => 'product.php');
}
// Returning the query after it has been modified
return $query;
}
add_action('pre_get_posts', 'SearchFilter');
Explaining the code:
The "post_type" will limit this filter for page post type only.
The condition Will limit this filter to only work on the search queries and not every time the posts query is called.
$query->is_search && $search_template
These parameters will filter the returned posts by the meta_key "_wp_page_template" which contains the page template, so we only return the pages with the page template "product.php".
$query->set('meta_key' => '_wp_page_template',);
$query->set('meta_value' => 'product.php');

Related

WordPress change search results

I have got the problem that my WordPress theme ("Sydney") is displaying the search results wrong. Instead of just listing posts I want to display WooCommerce products only.
Those should be ordered in a nice grid as shown in this picture:
.
At the moment the results are listed like this
.
How can I change the way the search results are displayed?
At the moment my search.php is looking like this:
<?php get_header(); ?>
<div id="primary" class="content-area col-md-9">
<main id="main" class="post-wrap" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h3><?php printf( __( 'Search Results for: %s', 'sydney' ), '<span>' . get_search_query() . '</span>' ); ?></h31>
</h3>
</header><!-- .page-header -->
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/**
* Run the loop for the search to output the results.
* If you want to overload this in a child theme then include a file
* called content-search.php and that will be used instead.
*/
get_template_part( 'content', 'search' );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
Thank you guys a lot!
** UPDATE
The solution provided by kashalo partly worked, but search results still look slightly different from the product page and are listed in only one column instead of a grid.
** UPDATE
The solution suggested by Alexander Wigmore looks almost the way I wanted it so look like. The only Problem with copying the page.phpin the search.phpis that the products are still getting displayed wheird, but not when they are displayed in the category they fit in. For example: When searching for saat the results are displaying the products at first with text only, but normaly under the Saatgut category.
my advise is not to directly modify the search file of wp or theme template but is a good practice to work with the child theme. For the design display of the result you can play a little with css to customize it as you want, instead for the results to show only the products you can use any filter builded on the theme you are using (if it have) or create yourself a function to filter the results (also called hooks).
An example for filtering results is like the above code:
add_action( 'pre_get_posts', 'filter_woocommerce_products' ); // the hook
function filter_woocommerce_products( $query ) { // the function
if( ! is_admin() && is_search() && $query->is_main_query() ) {
// verify if can use the search function and the search string is from the search call
$query->set( 'post_type', 'product' ); // filter the posts with type products that corrispond to woocommerce products.
}
}
and this function must be saved on the function.php file of your theme or the child theme.
For more information i suggest you to read more about child theme and the pre_get_posts documentation on the wp page.
Hope it helps and feel free to ask.
You'll want to modify the way search is handled by Wordpress.
This can easily be done by adding an "action", add the below code to your functions.php file.
function searchfilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type',array('product'));
}
return $query;
}
add_filter('pre_get_posts','searchfilter');
This will then make the search page only show products. It's worth noting that you can add other post types back in via the array in this line: $query->set('post_type',array('product'));.

Filter the posts by a specific author

I want to display only one author's post say "abc_author" .
i have use the validation using if clause but THE PROBLEM is that i get pagination below the post where page 1 is blank (as an other author post is skipped) , page 2 (as an other author post is skipped) is blank and soon i get my desired post on page 6 . commenting the verbosa_pagination() hide the pagination navigation but the starting pages are blank and
I am a new to WordPress , i have also tries different plugin but they facilitate me to filter it on pages not post (though i can change the home page to a particular page but i want it on default blog post for few reasons )
<?php if ( have_posts() ) : ?>
<div id="content-masonry">
<?php /* Start the Loop */
while ( have_posts() ) : the_post();
if(get_the_author()=='abc_author')
{
get_template_part( 'content/content', get_post_format() );
}
endwhile; ?>
</div> <!-- content-masonry -->
<?php verbosa_pagination();
else :
get_template_part( 'content/content', 'notfound' );
endif;
Can you please try this code before the while loop?
<?php query_posts( array( 'author' => 'your author ID' ) ); ?>
Ref: https://developer.wordpress.org/reference/functions/query_posts/#usage

Next and previous post link in same terms custom taxonomy

I've created a custom taxonomy => cat-blog in my custom post => blog, cat-blog have 4 terms and each terms have list of post belong to that term
Example of terms:
- City Updates (4 post belong)
- Home Tips (6 post belong)
- Real Estate Guide (8 post belong)
- Real Estate Industry (9 post belong)
and using this query
<?php
$query = new WP_Query(array('posts_per_page' => 2, 'post_type' => 'blog', 'blog-cat' => get_the_term_list( $post->ID, 'blog-cat' )));
while ($query->have_posts()) : $query->the_post();
?>
<?php
// content here
?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php
?>
to display 2 post in same category,
AND I just want to put next and prev pagination, so I can navigate the rest of the post, belong to that term.
Don't ever change the main query for a custom query on archive pages and on the home page. The main query already does what you want to do. Trying to run a custom query to try and get the same result is like reinventing the wheel. It also causes problems with pagination
SOLUTION
First, remove your custom query, and return to the main loop. The following is all you need in your taxonomy.php
if( have_posts() ) {
while( have_posts() ) {
the_post();
//REST OF YOUR LOOP
}
}
Use pre_get_posts in conjuction with the conditional tags if you need to alter the main query. For instance, if you need 2 posts per page on your taxonomy page, do the following in functions.php
function so26499451_custom_ppp( $query ) {
if ( !is_admin() && $query->is_tax() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '2' );
}
}
add_action( 'pre_get_posts', 'so26499451_custom_ppp' );
You can now paginate as normal without any problems. You will now see two posts from the specific term that you clicked on per page on your taxonomy.php.

Display all posts ordered by date

By default WordPress homepage shows latest posts. How can I display the latest posts on a page that is not the homepage ?
My first goal "GOAL A" is for the homepage to display a specific category called "popular posts" (instead of latest posts).
"GOAL B" is to have a link on the menu to ALL posts ordered by date, aka "the latest posts".
I have accomplished GOAL A with code below. How can I accomplish "GOAL B" ? I can make a category called "New" and make that a link on the menu, but how can I make it display all posts ordered by date ? Or is there a better method ?
.
"GOAL A" CODE: display specific category on homepage
function popular_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category_name', 'popular' );
}
}
add_action( 'pre_get_posts', 'popular_category' );
I think the best here will be is to create a static front page with a blog page. This seems to be fit for what you are trying to do
Here is how:
STEP 1
You should delete the code in your question. This will not be necessary here
STEP 2
Make a copy of your page.php (or index.php) and rename it front-page.php. Open it up, and replace the loop with a custom query which will only display posts from the desired category. Unfortunately, pre_get_posts does not work on a static front page, so here you will have to make use of a custom query.
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
// the query
$the_query = new WP_Query( 'category_name=popular&posts_per_page=10&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Just remember, for paging on a static front page, you have to use page, not paged as you would for all other custom queries.
STEP 3
Make a copy of your index.php and rename it home.php. This will be your blog page template
STEP 4
You can now set your static front page and blog page in the back. You should have a read here about setting up a static front page and a blog page

Wordpress - Display Limited Post From A Specific Category

In wordpress i need to display post on home page from a specific category.
Need Five Post With Title
Category Is News
Display On Main home page
i use plugin business news but when i activate this plugin nex gen gallery plugin will be conflict now i dont know how to resolve this please suggest.
Following is the code to display post of a specific category. To display on home page, you will have to make following changes in your index.php file.
$query = new WP_Query( array('category_name'=>'news', 'posts_per_page'=> '5') );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;

Categories