WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] posts between certain time

[Resolved] posts between certain time

  • Hello!

    i want to show only posts from a certain date to another date and the dates doesn’t match the beginning and end of the month. I know I can use monthnum=7&year=2005 in my WP_Query, but i want to know if I can also use something more complicated like: day<=8 ? Do I have to make two or more different queries for different months or is there some simple solutions that i’m not aware of?

Viewing 5 replies - 1 through 5 (of 5 total)
  • I don’t know of a way to combine queries, and to the best of my knowledge WP queries can’t accept comparison operators (well, other than =). I think your best option is to create a custom SQL query which gathers posts in the specified range; then you would pass the query through a modification of the older (pre 1.5) Loop initialization.

    So here’s an example SQL query:

    <?php $my_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_date > '2005-06-25 00:00:00' AND post_date < '2005-07-10 00:00:00'"); ?>

    This would select any posts with a date after (greater than) 2005-06-25 (June 25th) but before (less than) 2005-07-10 (July 10th). Then for the modded “loop”:

    <?php if($my_posts) : foreach($my_posts as $post) : setup_postdata($post); ?>

    ~ post-related template tags, html and other content go here~

    <?php endforeach; endif; ?>

    thanks, the time stuff works fine now but now i’m into some new problems.
    with the same query, i’d like to check wether the post is in certain category but the category is in another table and i don’t know a way to make query from two tables at the same time.

    another question which is about themes: which parameter defines what picture file is included in that line:
    #page { background: url(“http://blog.mobi.ee/wp-content/themes/default/images/kubrickbgwide.jpg”) repeat-y top; border: none; }
    i’d like to have kubrickbgwide.jpg in some places and kubrickbg.jpg in the other pages.

    ” i’d like to check wether the post is in certain category”

    <?php $my_posts = $wpdb->get_results("SELECT * FROM $wpdb->post2cat, $wpdb->posts WHERE category_id = '1' AND post_id = ID AND post_date > '2005-06-25 00:00:00' AND post_date < '2005-07-10 00:00:00'"); ?>

    Change 1 in category_id = '1' to the numeric category ID. You may also want to exclude Pages, as well as drafts and private posts, so slip in an evaluation of the post_status column:

    <?php $my_posts = $wpdb->get_results("SELECT * FROM $wpdb->post2cat, $wpdb->posts WHERE category_id = '1' AND post_id = ID AND post_status = 'publish' AND post_date > '2005-06-25 00:00:00' AND post_date < '2005-07-10 00:00:00'"); ?>

    For the latter issue, this may provide an initial idea:

    http://wordpress.org/support/topic/25616#post-145662

    If you need further help, I’d suggest posting another topic. It can get confusing for others looking for solutions when multiple, unrelated issues are covered in the same thread.

    thanks – the main problem is solved now, but the sidebar problem still remained. i’ll go and make another topic about it because unfortunately the link you gave doesn’t give me the information i want to have.
    thanks again!

    I have used the custom query method and setup_postdata as described above. This works fine for standard post properties (title, permalink etc), but the post metadata is empty. Does anyone know how I can get the post metadata?

    I think the problems comes from the $post_meta_cache being empty, but I can’t work out how to fill it.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] posts between certain time’ is closed to new replies.
Skip to toolbar