Display all posts ordered by date - php

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

Related

How to show only posts with certain category in wordpress theme?

I was wondering if there is a way to change this code to only display posts from certain category created in wordpress. Now it displays every recent post. Let's say I would create "News" category in wordpress and I want this piece of code to display only News posts.
Thanks for help
<?php
if( have_posts() ){
while( have_posts() ){
the_post();
get_template_part( 'template-parts/content', 'koncert');
}
}
?>
You can override the usual query that wordpress uses and create a custom one;
https://developer.wordpress.org/reference/classes/wp_query/
usually though for just displaying a category you can just open the category slug and provided your template has the correct archive page it will display the posts for that category.
If not use something similar to below. You can further refine your search parameters like number of posts and post types as defined in the link above.
<?php
//refine your query to the category you desire either a slug(example below) or category id
$args = array(
'category_name' => 'my_category_slug',
);
//create the query using the arguments
$query = new WP_Query($args);
?>
//create the loop to show the posts
<?php if($query->have_posts()): ?>
<?php while($query->have_posts()): $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content(); ?></div>
<?php endwhile; ?>
<?php endif; ?>

Wordpress - Display only pages with specific template in search results

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');

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

WordPress pagination gives 404 unless I set "Blog pages show at most" to 1 in reading

I am developing locally on MAMP using "Custom Post Types" and "Custom Fields" plugins. I have a custom post type of "Products" and a custom taxonomy of "Types."
I'm trying to use my pagination in "Taxonomy.php" because that's where I'm going to display my custom post types of "products" with my custom taxonomy "types" and use conditionals to generate different content on that same taxonomy.php.
This is the code in taxonomy.php that I got from css-tricks:
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=3&post_type=products'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
// my content from my custom field that I want paginated
<p><?php the_field('image'); ?></p>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
This question has been asked a lot around the web and I have read at least 30 different articles and blogs about this and still can't seem to find a solution. With this method of pagination I got from css-tricks the only problem is I keep getting a 404 error message whenever I try to go to the next page unless I set my "Blog pages show at most" to 1 in settings > reading. If I do that then it works, otherwise it gives me the 404 page. My permalinks are set to Post Name, by the way.
So I can see my pagination here:
http://localhost/MyWebsite/types/cars/
but when I get here I get the 404 page.
http://localhost/MyWebsite/types/cars/page/2/
I have read about the Flush Method but that does not work. I only have 2 plugins activated (custom post types and fields) and those are not the problem. How can I fix the 404 error?
I have diagnosed the problem further and I figured out whats going on. This is now my code in terminology.php:
<?php
$args = array(
'post_type' => array( 'products' ),
'posts_per_page' => 5,
'paged' => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1
);
$products_query = new WP_Query( $args );
$temp = $wp_query;
$wp_query = null;
$wp_query = $products_query;
while ($products_query->have_posts()) : $products_query->the_post();
?>
// content to display
<p><?php the_field('image'); ?></p>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
Bear with me because this is a little bit confusing. I'll try my best to clearly explain the difference between "Blog pages show at most" in settings > reading and my posts_per_page arguments.
I have a category of Honda with a sub-term of Accord and I have 3 products of my custom post type assigned to Accord
Now if I changed my posts_per_page argument what that does is no matter how many "Products" I have under "Accord" it will always demonstrate that amount even if you do not have enough.
So if I set it to 5 and I have 3 products it will still show 5 either repeated or from another category. When I click on the next pagination the pages will only go as far as however many items that I have. That is if "Blog pages show at most" is set to 1, which brings me to my next point.
Now the difference in changing "Blog pages show at most" is that even if I have my posts per pages set to 5 and I have only 3 "Products" but I change my "Blog pages show at most" to 3 it will not go to the next page because I only have 3 "products" in accord term.
If I were to change my "Blog pages show at most" to 1, I would be able to go forward 3 pages because I have 3 items.
It's sort of like changing "Blog pages show at most" changes the core of WordPress so no matter how many you put to display in the posts_per_page args it will only really count however many you set in "Blog pages show at most".
It seems like the pagination count is based upon "Blog pages show at most", not posts_per_page. I can have posts per page set to 10 and "Blog pages show at most" set to 2 and it would show 10 but only count as if there were really 2 on each page meaning I would only be able to go to the 2nd page of my "Accord" term that has 10 displaying in both of them.
Get the pattern? WordPress counted 2 products per page even though it was displaying 10. So it told the pagination that it can't go past page 2 because I only have 3 products.
But if you have a custom post type products and a taxonomy types you will show your content on: archive-products.php and taxonomy-types.php you could use pre_get_posts action to modify the query if you need and then write the markup you want on those files, example:
Put this on your functions.php
<?php
add_action( 'pre_get_posts', 'mr_modify_archive_taxonomy_query' );
function mr_modify_archive_taxonomy_query( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
if ( is_post_type_archive( 'products' ) || is_tax( 'types' ) ) {
$query->set( 'posts_per_page', 3 );
}
}
}
?>
an use a default loop
<?php
if ( have_posts() ):
while ( have_posts() ): the_post();
printf( '<p>%s</p>', get_the_field( 'image' ) );
endwhile;
previous_posts_link('« Newer');
next_posts_link('Older »');
endif;
?>

Problem with pagination in Wordpress custom template

I created a new template in Wordpress to show the latest articles. After, I will sort them in other ways. My problem is I can not use the pagination correctly.
The next page and previous page are working ok, but the posts are not changed. The first three are the only that are shown.
Can you give me a clue about this ?
<?php
/*
Template Name: Latest Prizes
*/
get_header();
// The Query
query_posts( 'posts_per_page=3' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
posts_nav_link(' · ', 'previous page', 'next page');
// Reset Query
wp_reset_query();
get_footer(); ?>
From the Wordpress Codex on query_posts():
...
For example, on the homepage, you would normally see the latest 10 posts. If you want to show only 5 posts (and don't care about pagination), you can use query_posts() like so:
query_posts( 'posts_per_page=5' );
Change your query_posts() to this:
query_posts(array('posts_per_page' => 3, 'paged' => get_query_var('page')));

Categories