Support » Fixing WordPress » Speed optimization of $wpdb->get_results

  • Hello,

    I’m using this code to generate all my custom field values from all posts for custom field named “autor”.

    <?php $autor = $wpdb->get_results("SELECT meta_value AS autor FROM wp_posts, wp_postmeta WHERE post_status = 'publish' AND meta_key = 'autor' GROUP BY meta_value ORDER BY meta_value") ?>
    <ol>
    <?php foreach( $autor as $autori ) : ?>
    <div class="autor">
    <li><a href="http://simplynote.me/?s=<?php echo $autori->autor ?>"><?php echo $autori->autor ?></a></li>
    </div>
    <?php endforeach ?>
    </ol>

    It works okey, but the problem is that it takes a couple of seconds more (like, the page is loading for 5-10 seconds) for server to load that particular page (www.simplynote.me/autori). Is there any way to optimize this request?

Viewing 4 replies - 1 through 4 (of 4 total)
  • That function makes one database call so should not slow down your site at all. There is probably something else causing the slow loading. What is the url of your site?

    Looks like your missing a join between wp_posts and wp_postmeta

    Maybe you want to add ‘ AND ID = post_id ‘ to avoid returning the product of the two tables and thousands of rows.

    Try "SELECT meta_value AS autor FROM wp_posts LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE post_status = 'publish' AND meta_key = 'autor' GROUP BY meta_value ORDER BY meta_value"

    Thanks! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Speed optimization of $wpdb->get_results’ is closed to new replies.