WordPress.org

Ready to get started?Download WordPress

Forums

a question about WP_query (8 posts)

  1. lostheaven0809
    Member
    Posted 10 months ago #

    i used wp_query class to get posts from a lot of different categories. after that, i checked and my web need 187 queries to database. it's terrible. i found out wp_query class need 5 query to database when it runs. is it right?
    what function i recommend to use when i want to get posts from a lot of different categories?

  2. Pioneer Valley Web Design
    Member
    Posted 10 months ago #

    Have you run any site optimization tools? that is a lot of queries, but not unheard of...

    Also, you can reduce that by not loading say, 187 posts on a page - are you paginating the posts?

    site link?

  3. lostheaven0809
    Member
    Posted 10 months ago #

    i design my theme by myself. not use any outside plugin.
    i code:

    $query = new WP_Query(array('posts_per_page' => 10, 'category_name' => 'tin-tuc'));
    ....
    wp_reset_postdata();
    $query = new WP_Query(array('posts_per_page' => 10, 'category_name' => 'tai-lieu'));
    ....
    wp_reset_postdata();

    and a lot of alike snippets. i get all of them out and then put into one by one. i found out every snippet need 5 query to database.

    site link: http://www.dientudangkhoa.com/

    thanks.

  4. Pioneer Valley Web Design
    Member
    Posted 10 months ago #

    Do this:

    Add this to the footer, just above </body>:

    <div><?php echo get_num_queries() ?> queries in <?php echo timer_stop(0) ?> seconds</div>

    This will give you the # of queries on each page (with footer, of course) and how long in seconds to make them...

    If on a shared host, this can vary (the time)...report back so we can see..

  5. lostheaven0809
    Member
    Posted 10 months ago #

    it put in the footer tag. you can see at the Menu section in footer

  6. Pioneer Valley Web Design
    Member
    Posted 10 months ago #

  7. lostheaven0809
    Member
    Posted 10 months ago #

    i think validator.w3.ord website can only check html error. not related to query to database. if it have error with html, it will not affect to php and mySQL.

  8. Pioneer Valley Web Design
    Member
    Posted 10 months ago #

    You should try to make a query and follow that with get_categories...once you have pulled the posts, run the functions but only when needed (per page template in use)

    http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_2

Reply

You must log in to post.

About this Topic

Tags