WordPress.org

Ready to get started?Download WordPress

Forums

Speed optimization of $wpdb->get_results (5 posts)

  1. bumTomica
    Member
    Posted 1 year ago #

    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?

  2. tomontoast
    Member
    Posted 1 year ago #

    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?

  3. ianhaycox
    Member
    Posted 1 year ago #

    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.

  4. tomontoast
    Member
    Posted 1 year ago #

    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"

  5. bumTomica
    Member
    Posted 1 year ago #

    Thanks! :)

Topic Closed

This topic has been closed to new replies.

About this Topic