WordPress.org

Forums

Too Many Queries (3 posts)

  1. andymike
    Member
    Posted 8 years ago #

    I am currently using this code to query specific posts by month and year in
    a page template, so I have to repeat this query 12 times for every year
    everytime the page is loaded:

    <?php query_posts('cat=2&year=2006&monthnum=1');
    if ($wp_query->post_count >= 1)
    {
    ?>
    <---do stuff-->
    <?php
    }
    ?>

    I am worried that this is too many queries. Is there a better way of doing
    this? Perhaps something along these lines:

    <?php query_posts('cat=2&year=2006');
    if ($wp_query->month_num = 1) && ($wp_query->post_count >= 1)
    {
    ?>
    <---do stuff-->
    <?php
    }
    ?>

    I know month_num won't work but is there any way of getting the month number so I can pass it in the PHP if statement? That way I only have to run one query for each year.

    Thanks

  2. muelheim
    Member
    Posted 8 years ago #

    You have to build a new sql-query like

    SELECT COUNT(*), year, month FROM texte GROUP BY year, month

    which returns an array with count, year and month in just one query

  3. andymike
    Member
    Posted 8 years ago #

    Thanks muelheim,

    Could you explain a little bit further how to build the sql-query?

    I appreciate your help.

Topic Closed

This topic has been closed to new replies.

About this Topic