Make Custom Post Archive in Ascending Order - php

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;

Related

How can I make so that the order of this loop's output is random?

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(); ?>

Wordpress wp_query displaying past post limit

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

one page wordpress not applying templates when useing The_content

I am building a one page WordPress template. I have created a simple loop that pulls all the pages and displays each one in succession on the same page. It displays the content for each page but dose not apply the template that has been attached to the page.
Here is the loop...
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
When you use the_content, the function will only return the value for the filed post_content within the table. So the code is working as it is expected.
Try the following
if ( have_posts() ){
while ( $the_query->have_posts() ) {
$the_query->the_post();
locate_template( get_template_slug( $post->ID ) )
}
}
This code uses the function locate_template to find the template file, and get_template_slug to get the name of the template associated with each page.
Let me know if this helps.

wordpress query_posts alternative

I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!

How to display more listings in WordPress

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.

Categories