How to Display all posts on a page in WordPress? - wordpress

I want to show all the posts of my wordpress blog on a single blog page, for some reason the solutions that I am finding in Google are 5-6 years old, I was wondering which is the best and easiest way to do it ?
Thanks

I think this will be the best one in my case ;)
$args = array(
'posts_per_page' => -1,
'post_type' => 'page',
'orderby' => 'DESC'
);
$home_query = new WP_Query($args);
if($home_query->have_posts()): while($home_query->have_posts()) : $home_query->the_post();
get_template_part('pages');
endwhile;
else:
get_template_part( 'no-results', 'home' );
endif;
and in pages.php
<section class="page-<?php echo get_post_type();?>-<?php the_ID();?> id="<?php echo $post->post_name;?>">
<div class="container">
<?php
the_content();
?>
</div>
</section>

Related

I need 2 posts, but, only shows 1 - WP_Query

I'm using this wp_query. I want to show 2 posts on my sidebar, but, it is showing only one - the loop have any wrong config? thank you!
<?php
$destaque = new WP_Query('post_type=post&posts_per_page=1&cat=2,3,4,5');
if($destaque->have_posts()):
while($destaque->have_posts()):
$destaque->the_post();
?>
<div class="col-md-12">
<?php get_template_part('content','homepost'); ?>
</div>
<?php
endwhile; wp_reset_postdata();
endif;
?>
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'category__not_in' => array(1),
'category__in' => array(2,3,4,5),
'offset' => 1
);
$secundarias = new WP_Query($args);
if($secundarias->have_posts()):
while($secundarias->have_posts()):
$secundarias->the_post();
?>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
Don t know why you have two queries, but it would simply be
$new_query = new WP_Query();
$new_query->query('post_type=post&showposts=2');
or in your example
('posts_per_page=2');
On your first query you set that to "1".
Use a single query with that before your loop and it should work.
Also check your categories - sometimes it is just

Display pages inside post loop by category ID

I've figured a way to enable adding categories to a PAGE (not a post). And I was just wondering if there was a way to display PAGES in a post loop, this is my code:
<?php query_posts('cat=540'); ?>
<div class="blog_module">
<?php if(has_post_thumbnail()) {
the_post_thumbnail(array(150,150));
} else {
echo '<img class="alignleft" src="'.get_bloginfo("template_url").'/images/empty_150_150_thumb.gif" width="150" height="150" />';
}
?>
<div class="entry">
<h3 class="blog_header"><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<a class="button_link" href="<?php the_permalink(); ?>"><span>Read More</span></a>
</div>
</div>
However, this displays:
Which isn't what I want, it ONLY DISPLAYS POSTS and not the pages I have assigned to Category ID 540.
Please could someone help with a loop that would display pages that have been assigned to a category.
Thank you in advance.
Sure its easy. I guess the following should work (untested):
query_posts(array('cat'=>540,'post_type'=>page));
You can also use WP_Query which is more flexible than query_posts:
$q = new WP_Query(array('cat'=>540,'post_type'=>page));
while($q->have_posts()): $q->the_post();
// your post here
endwhile;
This should be even more flexible:
$args = array(
'post_type' => 'page',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => 540
),
),
);
$query = new WP_Query( $args );
If you need posts AND pages try this:
'post_type' => 'any'
All information you need you can find on these links: https://developer.wordpress.org/reference/functions/query_posts/
https://codex.wordpress.org/Class_Reference/WP_Query

Wordpress order custom post type

I'm hoping someone can help me with this. I'm new to php so it is very much learning on the job.
I am customising an existing Wordpress theme but I am facing a problem with the themes advanced search/results.
The theme has no way to order search results and I know that the default order for Wordpress is by date. Currently if I perform a search, the results are displayed in date order but I need the results to be price high to low.
The current code is as follows
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<article id="post-0" class="post error404 not-found">
<h1 class="posttitle"><?php _e( 'Not Found', THE_LANG ); ?></h1>
<div class="entry">
<p><?php _e( 'Apologies, but no results were found for the requested property archive. Perhaps searching will help find a related post.', THE_LANG ); ?></p>
</div>
</article>
<?php endif; ?>
<div class="nvr-prop-container row">
<?php if( have_posts() ){ ?>
<div class="search-title twelve columns">
<h4><?php _e('Search Result', THE_LANG); ?> (<?php echo $wp_query->post_count; ?>)</h4>
</div>
<?php
$nvr_idnum = 0;
$nvr_typecol = "nvr-prop-col";
$nvr_imgsize = "property-image";
?>
<ul id="nvr-prop-search" class="<?php echo esc_attr( $nvr_typecol ); ?>">
<?php
while ( have_posts() ) : the_post();
$nvr_idnum++;
echo nvr_prop_get_box( $nvr_imgsize, get_the_ID(), 'element columns', $nvr_unit, $nvr_cursymbol, $nvr_curplace );
$nvr_classpf="";
endwhile; // End the loop. Whew.
?>
I then decided to try and sort the results so I created
$sort_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num date',
'order' => 'DESC',
));
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<article id="post-0" class="post error404 not-found">
<h1 class="posttitle"><?php _e( 'Not Found', THE_LANG ); ?></h1>
<div class="entry">
<p><?php _e( 'Apologies, but no results were found for the requested property archive. Perhaps searching will help find a related post.', THE_LANG ); ?></p>
</div>
</article>
<?php endif; ?>
<div class="nvr-prop-container row">
<?php if( $sort_properties->have_posts() ){ ?>
<div class="search-title twelve columns">
<h4><?php _e('Search Result', THE_LANG); ?> (<?php echo $wp_query->post_count; ?>)</h4>
</div>
<?php
$nvr_idnum = 0;
$nvr_typecol = "nvr-prop-col";
$nvr_imgsize = "property-image";
?>
<ul id="nvr-prop-search" class="<?php echo esc_attr( $nvr_typecol ); ?>">
<?php
while ( $sort_properties->have_posts() ) : $sort_properties->the_post();
$nvr_idnum++;
echo nvr_prop_get_box( $nvr_imgsize, get_the_ID(), 'element columns', $nvr_unit, $nvr_cursymbol, $nvr_curplace );
$nvr_classpf="";
endwhile; // End the loop. Whew.
?>
Now when I perform a search, the posts are sorted based on price which is fantastic but... now regardless of how I search all of the site posts are now being displayed.
I felt I was so close to finding a solution but I would very much grateful if someone cold advise me with this.
Kind regards
S
Have you tried to order only by the 'meta_val_num' without the 'date' like:
$args = array(
'post_type' => 'product',
'orderby' => 'meta_value_num',
'meta_key' => 'price',
);
$query = new WP_Query( $args );
Code from: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
I think you have to add the Search Parameter and probably use the new meta_query structure:
$search_query = get_search_query();
$sort_properties = new WP_Query(array(
'post_type' => 'properties',
's' => $search_query,
'orderby' => 'meta_value_num',
'meta_query' => array(
'key' => $nvr_initial.'_price',
'value' => $nvr_price,
)
));
I remember having some trouble, not using "meta_query" => array() for multiple conditions.

WP Custom taxonomy pagination

I have a custom taxonomy (support) and a custom post type (question) both related.
In my taxonomy-support.php template file, I use the following query:
<?php
$current_category = get_term_by('id', get_queried_object()->term_id, 'support');
$questions = new WP_Query(array(
'post_type' => array('question'),
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => ((get_query_var('paged')) ? get_query_var('paged') : 1),
'nopaging' => false,
'tax_query' => array(
array(
'taxonomy' => 'support',
'terms' => array($current_category->term_id)
)
),
'orderby' => 'menu_order',
'order' => 'ASC'
));
?>
And the loop
<?php if ($questions->have_posts()): ?>
<ul>
<?php while ($questions->have_posts()) : $questions->the_post(); ?>
<li>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</ul>
<div class="clearfix">
<div class="pull-left">
<?php previous_posts_link('← ' . __('Previous', 'my-theme' ), $questions->max_num_pages); ?>
</div>
<div class="pull-right">
<?php next_posts_link(__('Next', 'my-theme') . ' →', $questions->max_num_pages); ?>
</div>
</div>
<?php endif; ?>
As you can see I defined 2 posts per page.
When I visit the page, it shows me 2 posts then I go to page 2, it still works but when I go to page 3 or up, it shows 404.
Any idea?
I'm using WordPress 3.8.2 with no plugin installed.
Thanks
I was happy to find your post as I was fighting for the exact same issue (less happy to find no answer yet ;) ).
Anyway I finally found something interesting here: https://wordpress.org/support/topic/set-number-of-posts-for-custom-taxonomy
This made the trick for me.
I reckon you were into the situation where the value for your global posts_per_page option (as set up in the admin) was greater than the one for your taxonomy, which made the system screw up.
Well, not that much when you think about it: during its lifecycle, having the global posts_per_page option as is, the request does not validate the "paginated" url, thus never even load your taxonomy-support.php template file, where you tell it that the posts_per_page should be different.
You then must tell it earlier, during the intialization process. The 'pre_get_posts' action is the one.
Hope it helps someone!

Displaying Custom Category loop in homepage

Thing is, In the homepage of my theme, I want to show post from different category in Different Div. Each DIV will contain 3 post from a category. I need a loop that can pick last 3 post from a specific Category. Can't find any suitable ans for it.
To explain things more easily, here is a demo picture of the Content section,
http://i.imgur.com/5QSzAIS.png
It will be a great help, if someone help me with the code !
<?php query_posts('cat=10&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This should get you started. You need to use this code twice. Where it says cat=10, you should enter your category ID (you can check this when you click on a Category from the admin panel, the the browser it will show something like this http://yourwebsite.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=4&post_type=post)
Where it says tag_ID is the ID of your category.
I'm currently using a different method on a page of the site I'm building which allows me to run multiple loops in one page and specify the category for each one. This method I personally like better as it is more straightforward to me, and I can define the category with the slug instead of the ID.
Instead of using have_posts() and such, you use WP_Query() after defining your array and then wp_reset_postdata() to end your loop. The benefit is that you can keep running loops this way.
I'm also loading the data from custom fields in my posts using get_post_meta, but this method will work without that stuff.
<div class="audioGrid">
<?php
$args = array( 'post_type' => 'post',
'category_name' => 'audio',
'posts_per_page' => 3,
'order' => 'DESC' );
$query1 = new WP_Query($args);
while ( $query1->have_posts() ) {
$query1->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'audio-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'audio_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Audio Grid ?>
<?php wp_reset_postdata(); ?>
<div class="videoGrid">
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'order' => 'DESC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'video-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'video_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Video Grid ?>
<?php wp_reset_postdata(); ?>
Another cool thing I'm doing is using a custom field to define the order of things and using meta_key and meta_value_num to get that number and force the order how I want, and since this site isn't complicated, defining the order this way is convenient. I just use leading zeroes to make it easy: 001, 002, 003, etc
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'meta_key' => 'video_order',
'orderby' => 'meta_value_num',
'order' => 'ASC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
Anyway, hope this helps if you need to use multiple loops to pull posts from different categories.

Resources