Considering the following code, I have the loop in my Wordpress blog which will order posts from oldest to newer.
<?php
$args = array(
'order' => 'ASC'
);
query_posts( $args );
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('content'); ?>
<?php endwhile; ?>
What I want is to create two links on which the user clicks and changes this parameter, from older to newer or newer to older.
I thought about using jQuery to achieve this but I don't know exactly how I'll change the PHP code based on which link the user clicks.
If you change your sort direction to a parameter, e.g
<?php
$args = array(
'order' => (isset($_GET['dir']) ? $_GET['dir'] : 'ASC')
);
query_posts( $args );
?>
then you can create a link like:
Newest to Oldest
Oldest to Newest
Related
I using wp ajax load more plugin While clicking the button it repeating the same previous post. How to fix it. Here I share my code in below:
<?php
$the_query = new WP_Query( array(
'posts_per_page'=>10,//on loading page i show 10 after click load more i want to show other posts
'post_type'=>'post-name',
'category_name' => 'A-E',
'orderby'=> 'title',
'order' => 'ASC',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
// here I print the following data
<?php
endwhile;
?>
<?php
echo do_shortcode('[ajax_load_more post_type="post-name" posts_per_page="10" category="a-e" button_label="Load More"]');
?>
can anyone fix it?
I just use this to solve my own problem:
while ( $query->have_posts() ) : $query->the_post();
$do_not_duplicate[] = $post->ID; // Store post ID in array
// Other loop actions could go here
endwhile; wp_reset_query();
$post__not_in = ($do_not_duplicate) ? implode(',', $do_not_duplicate) : '';
echo do_shortcode('[ajax_load_more post__not_in="'. $post__not_in .'" post_type="post-name" posts_per_page="10" category="a-e" button_label="Load More"]');
If you want to get the next set of pages with ajax-load-more you must use the offset parameter. The ajax-load-more plugin must get the next posts that WP_Query has. So both WP_Query and ajax-load-more must query the same pages. First alter the shortcode to be same as WP_Query, by adding order and orderby parameter:
[ajax_load_more post_type="post-name" posts_per_page="10" order="ASC" orderby="title" category="a-e" button_label="Load More"]
Then add offset=(get_query_var('paged') ? get_query_var('paged') : 1)*10 like this:
[ajax_load_more post_type="post-name" offset='.((get_query_var('paged') ? get_query_var('paged') : 1)*10).' posts_per_page="10" order="ASC" orderby="title" category="a-e" button_label="Load More"]
Note: Seems like ajax-load-more doesn't know of - so you must separate category or post_type by ,. Unless you have actually a category named a-e
You don't need to create a custom query. You are using a plugin that will generate it for you via their shortcode. As per their documentation, you can just create your settings for the shortcode.
See instructions here https://connekthq.com/plugins/ajax-load-more/docs/shortcode-builder/
Just take the generated shortcode and paste it into your page/post or in your php file with do_shortcode.
The plugin handles the query and the posts per page etc.
In my Wordpress site, my taxonomy-product-category.php template has the following loop for loading posts into the page:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
However, I want the posts to be randomly ordered rather than following a specific order like date added.
How can I modify this loop to do it?
You can use pre_get_posts to set random ordering to your taxonomy pages. Just note, random ordering duplicates posts between paged pages as each page is a new query and not an extension to one. This is unfortunately how random ordering works.
You can try the following
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() // Only targets the front end
&& $q->is_main_query() // Only targets the main query
&& $q->is_tax( 'product-category' ) // Only targets the product-category tax pages
) {
$q->set( 'orderby', 'rand' );
}
});
Change:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
To:
<?php
$args = array(
'cat' => YOUR CATEGORY ID,
'post_type' => YOUR CUSTOM POST TYPE,
'orderby' => 'rand'
);
$query = new WP_Query($args);
?>
<?php if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
I just started working with WP and I'm desperately trying to create my own theme. I currently have been banging my head for a good portion of the day trying to figure out how to limit my query to just two post on my main page. For some reason it will either display all my posts or if I vary the code to have an additional value you in the array 'categories' => 'events' only one post shows up. Thanks in advance for any help you can provide :)
<?php
$args = array('posts_per_page' => 2);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) :
$my_query->the_post();?>
<?php get_template_part( 'events-content', get_post_format() ); ?>
<?php endwhile; ?>
<?php alpha_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'events-content', 'none' ); ?>
<?php endif; ?>
<?php wp_reset_postdata();?>
There are two things wrong with your Query.
The first is that you have posts_for_page in your arguments when it should beposts_per_page.
The second error is you put $args inside an array when you created the new WP_Query(). It should be:$my_query = new WP_Query( $args );
I'm trying to make my custom post type archive page reverse the display order, and want to make it ASCENDING. This is my code:
<?php
while ( have_posts() )
{
the_post();
?>
--MY CODE--
<?php } ?>
I tried putting query_posts('order=asc'); before the while loop, but this caused my loop to draw from the regular posts, not my custom post type.
Any help would be appreciated! Thanks
Use a simple query here. Don't use query_posts, never ever. Use WP_Query. You should do
<?php
$args= array(
'order' => 'ASC',
'post_type' => 'NAME OF YOUR CPT'
);
$the_query = new WP_Query( $args );
?>
<?php while ( $the_query->have_posts() ) :$the_querythe_post(); ?>
You need to add your ordering to the existing query. The way you did it overwrote the query. Try this:
global $query_string;
query_posts( $query_string.'&order=ASC' );
while( have_posts() ): the_post();
// rest of your code
endwhile;
I'm having an issue with the listings in the WordPress site I'm working on.
I have three listings only showing up out of 6. I can't seem to figure out how to make all of them display. This is using the twentyeleven WordPress theme.
The arrows on the right are used to move the gallery back and forth. Only one more shows up on the right side.
Here's the code I believe is generating it.
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php if ( is_home() ) {
query_posts($query_string . '&cat=-3');
}
?>
<?php
$page_name="Articles";
$page=get_page_by_title($page_name);
//echo $page->ID;
query_posts( 'cat=-1,-2' );
?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
</div>
Any help would be great, thanks.
Change your query_posts() function to the following:
query_posts( 'cat=-1,-2&posts_per_page=6' ); // You can change the post_per_page variable as needed
However, I would suggest using an $args array instead of a querystring to make your query. The same query would look like this:
$args = array(
'cat' => array( -1, -2 ),
'posts_per_page' => 6
);
query_posts($args);
It is much more readable and easier to update. Also, it's worth mentioning, you are adding a negative operator to your categories. In the query_posts function, that will exclude a category. You may only be getting 3 posts because you are excluding posts from your query.