WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom queries (19 posts)

  1. bekar09
    Member
    Posted 5 years ago #

    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?

  2. bekar09
    Member
    Posted 5 years ago #

    any suggestions on the query i should write?

  3. Mark / t31os
    Moderator
    Posted 5 years ago #

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

    Care to elaborate?

  4. bekar09
    Member
    Posted 5 years ago #

    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?

  5. Mark / t31os
    Moderator
    Posted 5 years ago #

    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...

  6. MichaelH
    Member
    Posted 5 years ago #

  7. bekar09
    Member
    Posted 5 years ago #

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

  8. MichaelH
    Member
    Posted 5 years ago #

    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

  9. bekar09
    Member
    Posted 5 years ago #

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

  10. bekar09
    Member
    Posted 5 years ago #

    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?

  11. bekar09
    Member
    Posted 5 years ago #

    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]

  12. MichaelH
    Member
    Posted 5 years ago #

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

  13. MichaelH
    Member
    Posted 5 years ago #

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

  14. bekar09
    Member
    Posted 5 years ago #

    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.

  15. MichaelH
    Member
    Posted 5 years ago #

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

  16. bekar09
    Member
    Posted 5 years ago #

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

  17. MichaelH
    Member
    Posted 5 years ago #

    You'll have to look at the time parameters section of query_posts for that.

  18. bekar09
    Member
    Posted 5 years ago #

    You'll have to look at the time parameters section of query_posts for that.

    I went through it

    w= - week of the year (from 0 to 53) and uses the MySQL WEEK command Mode=1.
    but could'nt make it out. could you please frame the query. please MichealH. thanks.

  19. MichaelH
    Member
    Posted 5 years ago #

    If you are to return posts in the last 7 days then you will need the function described in the Return posts for various date ranges: section of the query posts article.

Topic Closed

This topic has been closed to new replies.

About this Topic