Wordpress Post order - php

I wanted to re-arrange my recent post in wordpress so they go Ascending/Descending.
Here is my code:
<ul>
<?php query_posts('cat=3,4,5&posts_per_page=5&order=ASC'); foreach ($post as $post) ?>
<li>
<span class="date"><?php the_time('M j') ?></span>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
Each post is pulled from different categories. View site here

Why don't you use the standard query_posts?
<?php
//The Query
query_posts('cat=3,4,5&posts_per_page=5&order=ASC');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
?>
This code should work, if there's another reason that you're using get_posts over query_posts your problem is likely to be your argument list - from what I can see you will need to change
get_posts('cat=3,4,5,numberposts=5&order=DESC&orderby=date')
to
get_posts('cat=3,4,5&numberposts=5&order=DESC&orderby=date')
as &'s are used to separate parameters.

try using "orderby" as well ...
see: http://codex.wordpress.org/Template_Tags/get_posts

I would put categories 3,4,5 under a parent category. Then you could just pull in the single category (the parent category). For example, if you're new parent category is 17, you would do:
<?php query_posts('cat=17&numberposts=5&order=DESC&orderby=date'); foreach ($post as $post) ?>
This will display posts in category 17 and any of category 17's children. Then sorting should happen as you'd expect it to.

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

How to get just one category and not all of them

Here is the problem:
I've been using this code to get other posts to display in the end of the page (as a list), and those posts should be in the same category as the post that a visitor is currently reading. It works fine, but the problem that occurs is when the post is assigned to multiple categories, then I have multiple listings which is not needed.
How can I restrict this , to just one category (by name, or by id or the first one), so that the loop shows me posts from just one category when post with multiple categories is displayed?
Many thx.
<?php global $post;
$categories = get_the_category();
foreach ($categories as $category) :
?>
<h3 class="naslovostalih">Ostali članci iz ove kategorije:</h3>
<ul class="clanciostalih">
<?php
$posts = get_posts('numberposts=10&category='. $category->term_id);
foreach($posts as $post) :
?>
<li>
<?php
$title = get_the_title($ID);
$link = get_permalink();
printf('<a class="linkpost" title="%s" href="%s">%s</a>', $title, $link, $title);
the_post_thumbnail('thumb-232');
echo '<div id="excerptcu">';
echo excerpt(25);
echo '</div>';
?>
<p class="more-link-wrapper2"><?php _e( 'Opširnije »', 'fearless' ); ?></p>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
You can add the category id to get_posts just as in your code
Instead of looping the categories try like below: here 3 is the category id
$posts = get_posts('category=3&numberposts=10');
Also if you want to retrieve the category id of specific category by name you can use
get_cat_ID( $cat_name )
Perhaps you could just take the first category in your foreach loop instead of iterating through all of them? You can do this by setting a variable like
$first = true;
Before your loop. Then, set your foreach loop in an "if" that checks to see if $first == false. If so, do the loop. In your loop, do your stuff and set $first = true and it will only iterate once.

Remove Category Uncategorized from Wordpress Loop

I want to list all post from a custom post type by category so I came up with this code (see below) but them I see this uncategorized category I want to remove. I tried in the query cat=$cat_id&exclude="1"&post_type=locations but it's not working. Any ideas? Thank You
<div class="entry-content">
<?php
$cats = get_categories();
foreach ($cats as $cat) {
$cat_id= $cat->term_id;
echo "<h2>".$cat->name."</h2>";
query_posts("cat=$cat_id&exclude="1"&post_type=locations");
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php ?>
<?php the_title(); ?>
<?php echo '<hr/>'; ?>
<?php endwhile; endif; ?>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div><!-- .entry-content -->
This is quite old, but I stumbled across it, so in case it is relevant to anyone, here is what I believe the answer is:
Since you are specifying each category by ID, you are trying to exclude the category that you are specifying when it has a term ID of 1. So in the first iteration of the loop, you are saying
cat=1&exclude=1&post_type=locations
Since you are specifying exactly what you are excluding, I believe the "inclusive" query value outweights the "exclusive" query value. If this is the case, you can instead do it like this (and even save a query!):
$cats = get_categories();
foreach ( $cats as $cat ) {
$cat_id = $cat->term_id;
if ( $cat_id == 1 )
continue; // skip 'uncategorized'
echo "<h2>".$cat->name."</h2>";
query_posts("cat=$cat_id&post_type=locations");
?>{the rest}<?php
}
Please note, I don't recommend the use of query_posts, and instead get_posts() or new WP_Query should be used instead. Further reading:
https://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/
`query_posts( 'cat=-3' );`
// 3 is category id you want to exclude
see more ref. about query_posts QUERY POSTS
$args = array('exclude' => array(1));
$categories = get_categories($args);
foreach($categories as $category) {
echo $category->name;
}
Please try this I Hope this works. Thank you.
The problem are your quotes
try this:
query_posts("cat=$cat_id&exclude=1&post_type=locations");
Hope this helps you.
TRY THIS ;
Go to Appearance then click on Widget,
Once widget opened, Click on Primary Widget area,
You will see "Category"
Just click on,Hold your left mouse button and move to
"Inactive widget " Group.
When you go back your website, You will not see Uncategorized anymore. :-))

What is the best method for creating your own Wordpress loops?

There seem to be three main ways to output content from Wordpress using its built-in functions, with WP_Query being the recommended one:
WP_Query
query_posts
get_posts
What are the differences between them? (I understand that WP_Query is the class, and the other two are methods).
What is the cleanest way to have multiple loops on the same page, without any of them interfering with each other?
I'm looking for examples of how you program your WP loops;
e.g. output 2 separate post lists by category, with attachments, meta data etc.
This is the best reference I found so far:
Define Your Own WordPress Loop Using WP_Query
I've used both WP_Query and get_posts. On one of my sidebar templates, I use the following loop to display posts from a particular category by using custom fields with a key of 'category_to_load' which contains the category slug or category name. The real difference comes in the implementation of either method.
The get_posts method looks like so in some of my templates:
<?php
global $post;
$blog_posts = get_posts( $q_string );
foreach( $blog_posts as $post ) :
setup_postdata( $post );
?>
<div class="blog_post">
<div class="title">
<h2>
<?php the_title(); ?>
</h2>
<span class="date"><?php the_time( 'F j, Y' ); ?> by <?php the_author(); ?></span>
</div>
<?php the_excerpt(); ?>
</div>
<?php endforeach;
?>
Where the WP_Query implementation looks like this:
$blog_posts = new WP_Query( 'showposts=15' );
while ( $blog_posts->have_posts() ) : $blog_posts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>" class="blog_post">
<div class="title">
<h2>
<?php the_title(); ?>
</h2>
<span class="date"><?php the_time( 'F jS, Y' ) ?> <!-- by <?php the_author() ?> --></span>
</div>
<div class="entry">
<?php the_content(); ?>
</div>
<p class="postmetadata"><?php the_tags( 'Tags: ', ', ', '<br />' ); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php endwhile; ?>
The main difference is that you don't have to reset the global $post variable and you also don't have to set up the post data by calling setup_postdata($post) on each post object when you use WP_query. You can also use the lovely have_posts() function on the WP_Query function, which is not available using get_posts().
You shouldn't use the query_posts() function unless you really mean to because it modifies the main loop for the page. See the docs. So if you're building a special page to display your blog on, then calling query_posts may mess up the page's loop, so you should use WP_Query.
That's just my two cents. My ultimate suggestion, your first choice should be WP_Query.
-Chris
From the WP docs for get_posts:
get_posts() can also take the parameters that query_posts() can since both functions now use the same database query code internally.
The only difference between the two functions is that get_posts returns an array with post records, while query_posts stores the records in the query object for retrieval by the template functions (has_posts, the_post, etc).
They both use the WP_Query object to execute the query.
Creating a second loop is covered in the Wordpress docs. There are some links there for other examples of multiple loops. You'll notice that everyone does it differently, but they all seem happy with their results.
WP uses an object called $wp_query for the main loop. We usually don't see this object because it's hidden behind have_posts() and the_post() that are only wrappers for
$wp_query->have_posts() and $wp_query->the_post()
If you want to modify the main loop you should use query_posts() before the loop.
If you want another loop you can reutilize the $wp_query object using query_posts() before that new loop. This can be done many times if needed.
If for some reason you need to keep the $wp_query object around THEN you should use WP_Query. And of course, because have_posts() and the_post() are wrappers for $wp_query object you can't use them with WP_Query. You should rather use $your_query_obj->have_posts() i.e.
$sidebar= WP_Query('category_name= sidebar');
while( $sidebar->have_posts() ): $sidebar->the_post();
the_title();
the_content();
endwhile;
A good case where WP_Query could be better than query_posts() is a left sidebar.
As the code loop for the sidebar will probably be put on top of the main loop, a query_posts() call will have changed the $wp_query object and also changed the main loop. In this case to use query_posts() at the sidebar code you will also need to use query_posts() before the main loop to query the proper content for that loop.
So using WP_Query for this case will keep $wp_query and therefore the main loop untouched.
But again, for a common case scenario query_posts() is a beautiful way to query your content:
query_posts('category_name=blog');
while( have_posts() ): the_post();
the_title();
the_content();
endwhile;

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

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.

Categories