Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query Category based on date WordPress

  • SOLVED

Hey,



I have created some categories, based on the name of the Week

Monday
Tuesday
Wednesday
etc...


I want on sidebar, to query every day, a different category, based on date!

On monday, i want to query mondays' posts, etc!

is there any possible way to do that?

Answers (4)

2012-10-23

Francisco Javier Carazo Gil answers:

Well, the first one is get the day:

$dw = date( "w", $timestamp);
$day = "";
switch($dw)
{
case 0:
$day = "Monday";
break;

case 1:
$day = "Tuesday";
break;

...

case 6:
$day = "Sunday";
break;
}


Francisco Javier Carazo Gil comments:

With this code, you can do directly in your sidebar the query:

query_posts('cat='.$day);

And then a typical loop.


Francisco Javier Carazo Gil comments:

For example:

<?php $day = getDay(); // the function ?>
<h3>Post of $day</h3>
<ul>
<?php query_posts('category_name=$day'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><br />
<?php the_excerpt(); ?></li>
<?php endwhile; ?><?php endif; ?>
</ul>


Basilis Kanonidis comments:

COuld You please reply, on a

Single Topic, as it is really hard to understand right now? I find your answer usefull, but like that, it gets really hard.


Francisco Javier Carazo Gil comments:

Tell me what exactly don't you understand.


Basilis Kanonidis comments:

Do it all, one post, step by step, please! :)

It is hard to understand like that


Francisco Javier Carazo Gil comments:

If you prefer send me login details via PM and I will do it.


Basilis Kanonidis comments:

Hey, you going to finish it, or should i move on to end it, with someone else? :)

2012-10-23

Kyle answers:

See here: http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category

query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );


That will output all posts for a category

so, this would be for Wednesday for example

query_posts( array ( 'category_name' => 'Wednesday', 'posts_per_page' => -1 ) );


Kyle comments:

Within the query posts array there you can set all sorts of other things like how to order it (most recent or oldest), # of posts to display, define the author, etc.


Basilis Kanonidis comments:

Read the post please.


Kyle comments:


Try this, it retrieves the current day of the week, and then queries categories with that term

$getday = the_date('l');
query_posts( array ( 'category_name' => '$getday', 'posts_per_page' => -1 ) );


Kyle comments:

If you do

$getday = the_date('l');
$q = new WP_query( array ( 'category_name' => '$getday', 'posts_per_page' => -1 ) );
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();

In here I can format the loop anyway you want. Get the title, the author, the date, the excerpt
}}


This is nice and neat, it automatically does the day of the week so you don't have to do a function 7 times :)

2012-10-23

John Cotton answers:


// Get the full name of the week
$dw = date( "l", $timestamp);

// Use it to retrieve the id of that term (assuming it's a category tax)
$term = get_term_by( 'name', $dw, 'category' );

// Get the posts - use more params if you need to
$posts = get_posts( array( 'cat' => $term->term_id ) );

// loop as normal

2012-10-23

Arnav Joy answers:

try this

$day_of_week = date("l");
$num_of_posts = 10;
wp_reset_query();
query_posts('category_name='.$day_of_week.'&posts_per_page='.$num_of_posts );
echo '<ul>';
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a></li>
<?php
endwhile;
endif;
echo '</ul>';
wp_reset_query();