How do I get wordpress to override a previous posts query? - php

I've got a page that has a category list at the top, and should normally list posts below it. The category list is created using:
<?php $display_categories = array( 4, 7, 8, 9, 21, 1); $i = 1;
foreach ( $display_categories as $category ) { ?>
<div>
<?php single_cat_title(); ?> //etc
</div>
<?php }
?>
However, this seems to make the post loop order posts by category. I want it to ignore category ordering and order by date in descending order. I've created a new WP_Query since according to the docs you can't use query_posts() twice, so just in case.
<?php $q = new WP_Query( "cat=-1&showposts=15&orderby=date&order=DESC" );
if ( $q->have_posts() ) :
while ( $q->have_posts() ) : $q->the_post(); ?>
the_title(); // etc
endwhile;
endif;
?>
However, this still seems to be ordered by category (the same order as the list above) and then by date, as opposed to just by date.

I've had problems with this before as well.
Try this:
<?php
global $post;
$myposts = get_posts( 'numberposts=5' );
foreach( $myposts as $post ) : setup_postdata( $post ); ?>
<div <?php post_class(); ?>>
<div class="title">
<h2>
<?php the_title(); ?>
</h2>
<p class="small"><?php the_time( 'F j, Y' ); ?> by <?php the_author(); ?></p>
</div>
<?php the_excerpt(); ?>
</div>
<?php endforeach;
?>
The important line is global $post;.
That should reset your global query. The setup_postdata($post) method is necessary to give you access to functions like the_author() or the_content().
-Chris

I don't have any experience with wordpress, but a couple of possibilities:
You define the "order" parameter twice in the string you're calling query_posts() with, I don't know if that causes a problem or not.
As well, "show" is not a valid parameter, you may have been looking for "showposts".
Parameters and their effects are described here: http://codex.wordpress.org/Template_Tags/query_posts#Parameters

query_posts is finicky sometimes. Try something like this and see if it works:
query_posts(array('category__not_in'=>array(1),
'showposts'=>15,
'orderby'=>date,
'order'=>DESC));
Since that's not the issue, try adding update_post_caches($posts) to the second loop, like this:
<?php $q = new WP_Query("cat=-1&showposts=15&orderby=date&order=DESC");
if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); update_post_caches($posts); ?>
the_title(); // etc
endwhile; endif; ?>
Supposedly this solves some plugin problems.

Related

Display Wordpress posts in Chronological (Ascending) Order

By default Wordpress shows all posts in reverse chronological order (with the newest post first).
I would like to display all of my wordpress posts in chronological order (with the oldest posts shown first).
I am trying to use a custom loop query to do this, however I cannot get it to work. What am I missing here?
<?php query_posts(array('orderby'=>'date','order'=>'ASC'));
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="postTitle"><?php the_title(); ?></div>
<div class="postContent"><?php the_content(); ?></div>
<?php endwhile; endif;
wp_reset_query();
?>
I thought this would be quite simple, although everything I have found to try I also cannot make work. thanks!
Using custom loop:
If you are creating a custom loop you might want to use WP_Query instead.
<?php
$the_query = new WP_Query([
'order'=>'ASC'
]);
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) :
?>
<div class="postTitle"><?php the_title(); ?></div>
<div class="postContent"><?php the_content(); ?></div>
<?php
endwhile;
/* Restore original Post Data */
wp_reset_postdata();
?>
<?php else: ?>
// no posts found
<?php endif; ?>
Using filters
Or another method is to alter the main loop using filters in your functions.php file.
function alter_order_of_posts( $query ) {
if ( $query->is_main_query() ) {
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'alter_order_of_posts' );
I suggest the filter path to avoid changing a lot of your current template.

Getting related articles in WordPress not working

I am trying to display all the posts with the same category in WordPress however its not displaying correctly and instead is just showing everything.
Here is the php code:
<?php
$related = get_posts( array(
'category_in' => wp_get_post_categories($post->ID),
'numberposts' => 3,
'post_not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<div class="post">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<h3><?php the_title();?></h3>
</a>
</div>
<?php }
wp_reset_postdata();
?>
Its taken from here: https://wordpress.stackexchange.com/questions/41272/how-to-show-related-posts-by-category
And if it helps here is the link to the website in question where the code isnt working:http://u1f8aki.nixweb23.dandomain.dk/cat-4-post-test/
The code in question is further down the page under the red text. You can see the category at the top in the breadcrumbs.
There are quite errors in your code, and I'm sure atleast one of them are the culprit in giving you the wrong result.
I've refactored your code with some comments explaining what and why has been changed:
<?php
// For readability, save our categories in a variable for later use.
// $post->ID has been replaced with get_the_ID(), $post might not be accessible depending if you're exposing $post as a global or not.
$categories = wp_get_post_categories(get_the_ID());
/*
Instead of using get_posts(), use the recommended Wordpress loop in the form of WP_Query().
We start by defining our arguments for the loop
*/
$args = array(
'category_in' => $categories, // here we use variable for readability
'posts_per_page' => 3, //numberposts and posts_per_page has the same function, but posts_per_page is the more common of the two (IMO)
'post__not_in' => array(get_the_ID()) // you were missing a '_', ie post_not_in instead of post__not_in
);
// Start the loop
$query = new WP_Query($args);
if($query->have_posts()): while($query->have_posts()): $query->the_post();
// No need to setup or reset postdata when using this method, it does it for you!
?>
<div class="post">
<a href="<?php the_permalink(); ?>">
<?php
if( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<?php
// the_title() actually takes opening tag and closing tags as arguments in its function. So add the <h3> code like this.
the_title('<h3>', '</h3>');
?>
</a>
</div>
<?php endwhile; endif; ?>
Long story short, your code prolly isn't working because of the miss-named arguments. If you don't feel like replacing your code with my example, just change your arguments from numberposts to posts_per_page, and post_not_in to post__not_in.
If it still isn't working, check what wp_get_post_categories(get_the_ID()) is returning for each post, and make sure all posts aren't sharing some category you missed.
Edit: numberposts is actually a valid argument, changed my answer to reflect this.

How to get post using a specific category

I'm just getting back into Wordpress and php after a few years away. I'm pretty ignorant when it comes to php syntax so making lots of mistakes.
I need to display a single post on a page, it's title and contents, and it needs to be specific to a category that I've created.
I grabbed this of the web and tried to change it, but I can't get it to work.
<?php query_posts('category_name=qaadrant&showposts=1');
while (have_posts()) : the_post();
// do whatever you want
?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
Any help much appreciate. Niall
Please use awesome WP_QUERY Class see the reference here:
https://codex.wordpress.org/Class_Reference/WP_Query
Using following code to get posts from category you selected. No-paging is set to make query faster:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'category_name' => 'qaadrant',
'no-paging' => true,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php echo get_the_title(); ?></h3>
<p><?php echo get_the_excerpt(); ?></p>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

Show category name above category posts

I'm trying to get the category name to show above posts. I'm customizing bones and am a little stuck.
Here's the code that has my posts displaying, and I have each in a category in the wp admin. I'm just not sure how to get the category to display above the appropriate posts.
Hope this makes sense. Thanks in advance!!
<?php
$args = array( 'post_type' => 'custom_type', 'posts_per_page' => 100 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) { ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php } else { ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php } ?>
It looks like you are doing a custom loop (as opposed to "The loop"), hence you'll need to pass the id of the post to get_the_category. However, judging by the docs, it seems that WP_Query->the_post() sets the global post.
So, if this does not work:
<h2><?php echo get_the_category() ?></h2>
You would do:
$postsQuery = new WP_Query( $args );
$posts = $postQuery->get_posts();
foreach ( $posts as $i => $e ) {
$category = get_the_category($e->ID);
}
https://developer.wordpress.org/reference/functions/get_the_category/
https://codex.wordpress.org/Class_Reference/WP_Query
https://codex.wordpress.org/The_Loop
You can use like <?php the_category();?>. This will returns the categories name with link. Hope this help you!
Hi I could not post in comments, but if above answer is working for you as you posted in comments but having issue with getting category as it give array due to many categories of a post, so you need to fetch first category from the array. Use below code snippet:
if ( ! empty( $category ) ) {
echo '' . esc_html( $category[0]->name ) . '';
}

Show posts filetered by tag on wordpress?

I want to query all posts that contain an specific tag, so, lookin on the codex ref, it says something like this should work:
$args = array( 'tag' => 'my-tag' );
$wp_query->query($args);
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
endwhile;
But it always returns false...
I was reading is not possible to do that, and I have to use the has_tag while i do a while over all the posts, is it true??
Any idea how to do this?
Thanks,
Very simple, just requires reading: http://codex.wordpress.org/Class_Reference/WP_Query
<?php $your_query = new WP_query(' tag_id=###' );
if ( $your_query->have_posts() ) : ?>
// This begins the loop to post the posts with your tag
<?php while ( $your_query->have_posts() ) : $your_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
// Closes loop
<?php wp_reset_postdata(); ?>
<?php else : ?>
// Loads error if no posts exists
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Categories