Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Still generating a lot of get_term_by queries #12

Closed
jb510 opened this issue Feb 22, 2014 · 2 comments
Closed

Still generating a lot of get_term_by queries #12

jb510 opened this issue Feb 22, 2014 · 2 comments

Comments

@jb510
Copy link

jb510 commented Feb 22, 2014

I'm thinking get_term_by (used for do_more_from_category() on widget.php:650 ) should perhaps be cached.

Found this Automattic/Edit-Flow#235 which seems like a useful example.

Is it worth writing a caching wrapper function for get_term_by like they did? I might give repurposing their code a go for this to see if it helps.

@jb510
Copy link
Author

jb510 commented Feb 22, 2014

Ok, perhaps cleaner, I'm trying something based of VIP's cached get_term_by. Not getting the result I was hoping for yet.

function wpcom_vip_get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw' ) {
    // ID lookups are cached
    if ( 'id' == $field )
        return get_term_by( $field, $value, $taxonomy, $output, $filter );

    $cache_key = $field . '_' . md5( $value );
    $term_id = wp_cache_get( $cache_key, 'get_term_by' );

    if ( false === $term_id ) {
        $term = get_term_by( $field, $value, $taxonomy );
        if ( $term && ! is_wp_error( $term ) )
            wp_cache_set( $cache_key, $term->term_id, 'get_term_by' );
        else
            wp_cache_set( $cache_key, 0, 'get_term_by' ); // if we get an invalid value, let's cache it anyway
    } else {
        $term = get_term( $term_id, $taxonomy, $output, $filter );
    }

    if ( is_wp_error( $term ) )
        $term = false;

    return $term;
}

wpsmith added a commit that referenced this issue Mar 20, 2014
@jb510
Copy link
Author

jb510 commented Mar 22, 2014

Cool. Looks like it's working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants