• Resolved bekar09

    (@bekar09)


    Is it possible to query posts based on the current date and then randomise the results using ‘&orderby=rand’.

    First I need to query the posts based on the current date and then apply ‘&orderby=rand’ to it? How do I do it?

Viewing 15 replies - 1 through 15 (of 18 total)
  • Thread Starter bekar09

    (@bekar09)

    any suggestions on the query i should write?

    How can it be both based on the current date and random, that doesn’t make sense to me..

    Care to elaborate?

    Thread Starter bekar09

    (@bekar09)

    suppose there are 10 posts for today. i want to show only these 10 posts in the random order. if i use orderby=rand, then it shows all posts in the random order irrespective of the date they were published. here if we can write a sub-query to find out the posts that are published for the current date and then randomize the order then it would be fine. is that possible?

    This works for me… (just tested).

    <?php
    	$today = getdate();
    	query_posts('year=' .$today["year"] .'&monthnum=' .$today["mon"] .'&day=' .$today["mday"].'&orderby=rand' );
    	?>

    I get a random order of posts from today only…

    Thread Starter bekar09

    (@bekar09)

    thanks t31os_ for your suggestion. but being a novice, its very difficult for me to frame the query for my template. I’m posting the query for my template. i would be thankful to you if you help me write the query.

    <?php $showpost = new WP_query(); $showpost->query('showposts='.$options['left_X_news'].'&cat=-'.$options['photos_id'].',-'.$options['videos_id'].',-'.$options['celebrities_id'].'&orderby=rand'); ?>

    See if this gets you there:

    <?php
    $today = getdate();
    $showpost = new WP_query();
    $showpost->query('showposts='.$options['left_X_news'].'&cat=-'.$options['photos_id'].',-'.$options['videos_id'].',-'.$options['celebrities_id'].'&orderby=rand&year=' .$today["year"] .'&monthnum=' .$today["mon"] .'&day=' .$today["mday"]);
    ?>

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

    Thread Starter bekar09

    (@bekar09)

    thanks a lot. that was the perfect solution. thanks to both t31os_ and MichaelH.

    Thread Starter bekar09

    (@bekar09)

    i have come up with a new question. suppose for a particular category, i want to arrange posts in the alphabetical order of the titles. i have gone through Alphabetizing_Posts. this works good. but if i want to show posts in a more organised way. for example if i want to display the list of posts as in this website, then how should i write the query?

    Thread Starter bekar09

    (@bekar09)

    See if this gets you there:
    <?php
    $today = getdate();
    $showpost = new WP_query();
    $showpost->query(‘showposts=’.$options[‘left_X_news’].’&cat=-‘.$options[‘photos_id’].’,-‘.$options[‘videos_id’].’,-‘.$options[‘celebrities_id’].’&orderby=rand&year=’ .$today[“year”] .’&monthnum=’ .$today[“mon”] .’&day=’ .$today[“mday”]);
    ?>

    Hi MichaelH,
    the above solution solved my problem, but leads to a new problem. If i don’t have posts for today then the above query returns nothing. is it possible to add an if condition that if the above mentioned returns nothing, then show posts from the last 7 days in the random order?

    how to achieve that? please help me.

    [moderated–bumps removed. Please refrain from bumping as per Forum Rules]

    Might want to look at the A to Z Index plugin.

    Also found this to list posts alphabetically, with each letter in separate section:
    http://wordpress.org/support/topic/148727?replies=14#post-666306

    Thread Starter bekar09

    (@bekar09)

    firstly sorry for the previous bumps. the A-Z Index plugin seems to be complicated. I think I’ll try with

    Also found this to list posts alphabetically, with each letter in separate section:
    http://wordpress.org/support/topic/148727?replies=14#post-666306

    MichaelH any suggestion for

    Hi MichaelH,
    the above solution solved my problem, but leads to a new problem. If i don’t have posts for today then the above query returns nothing. is it possible to add an if condition that if the above mentioned returns nothing, then show posts from the last 7 days in the random order?

    how to achieve that? please help me.

    thanks.

    if (! $showposts) {
    //put another query here
    }

    Thread Starter bekar09

    (@bekar09)

    thanks MichaelH,
    But I’m unable to get the current week. could you please elaborate. i’m a novice. thanks.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Custom queries’ is closed to new replies.