Making WordPress.org

Ticket #561: 561.2.diff

File 561.2.diff, 3.6 KB (added by hlashbrooke, 11 years ago)

Using multiple queries, but storing data in transient

  • wp-content/plugins/wc-post-types/wc-post-types.php

     
    256256                        'posts_per_page' => -1,
    257257                        'orderby' => 'date',
    258258                        'order' => 'desc',
     259                        'track' => 'all',
    259260                ), $attr );
    260261
    261262                $attr['show_avatars'] = $this->str_to_bool( $attr['show_avatars'] );
    262263                $attr['orderby'] = ( in_array( $attr['orderby'], array( 'date', 'title', 'rand' ) ) ) ? $attr['orderby'] : 'date';
    263264                $attr['order'] = ( in_array( $attr['order'], array( 'asc', 'desc') ) ) ? $attr['order'] : 'desc';
    264265
    265                 $speakers = new WP_Query( array(
    266                         'post_type' => 'wcb_speaker',
    267                         'posts_per_page' => intval( $attr['posts_per_page'] ),
    268                         'orderby' => $attr['orderby'],
    269                         'order' => $attr['order'],
    270                 ) );
     266                // Add a 'refresh' parameter to the URL to force the speaker list to refresh
     267                if( isset( $_GET['refresh'] ) ) {
     268                        delete_transient( 'wordcamp_speaker_ids' );
     269                }
    271270
     271                // Get saved speaker IDs of transient exists
     272                $speaker_ids = get_transient( 'wordcamp_speaker_ids' );
     273
     274                if( ! $speaker_ids ) {
     275
     276                        // Fetch all sessions
     277                        $session_args = array(
     278                        'post_type'   => 'wcb_session',
     279                        'posts_per_page' => -1,
     280                        );
     281
     282                        // Filter by track if specified
     283                        if( 'all' != $attr['track'] ) {
     284                                $session_args['tax_query'] = array(
     285                        array(
     286                            'taxonomy' => 'wcb_track',
     287                            'field'    => 'slug',
     288                            'terms'    => explode( ',', $attr['track'] ),
     289                        ),
     290                        );
     291                        }
     292
     293                        $sessions = get_posts( $session_args );
     294
     295                        // Get array of relevant speaker IDs
     296                        $speaker_ids = array();
     297                        foreach ( $sessions as $session ) {
     298                        $session_speaker_ids = get_post_meta( $session->ID, '_wcpt_speaker_id' );
     299                        $speaker_ids = array_merge( $speaker_ids, $session_speaker_ids );
     300                        }
     301
     302                        // Set transient with speaker IDs to prevent long page load times
     303                        set_transient( 'wordcamp_speaker_ids', $speaker_ids, 3600 );
     304                }
     305
     306                // Fetch all specified speakers
     307                $speaker_args = array(
     308                        'post_type' => 'wcb_speaker',
     309                        'posts_per_page' => intval( $attr['posts_per_page'] ),
     310                        'post__in' => $speaker_ids,
     311                        'orderby' => $attr['orderby'],
     312                                'order' => $attr['order'],
     313                );
     314
     315                $speakers = new WP_Query( $speaker_args );
     316
    272317                if ( ! $speakers->have_posts() )
    273318                        return '';
    274319
     
    279324
    280325                        <?php while ( $speakers->have_posts() ) : $speakers->the_post(); ?>
    281326
    282                                 <div id="wcorg-speaker-<?php echo sanitize_html_class( $post->post_name ); ?>" class="wcorg-speaker">
     327                                <?php
     328                                // Get list of speaker tracks for div class
     329                                $tracks = get_post_meta( get_the_ID(), '_wcpt_speaker_tracks', false );
     330                                $track_class = '';
     331                                foreach( $tracks as $track ) {
     332                                        $track_class .= ' ' . sanitize_html_class( $track );
     333                                }
     334                                ?>
     335
     336                                <div id="wcorg-speaker-<?php echo sanitize_html_class( $post->post_name ); ?>" class="wcorg-speaker<?php echo $track_class; ?>">
    283337                                        <h2><?php the_title(); ?></h2>
    284338                                        <div class="wcorg-speaker-description">
    285339                                                <?php echo ( $attr['show_avatars'] ) ? get_avatar( get_post_meta( get_the_ID(), '_wcb_speaker_email', true ), absint( $attr['avatar_size'] ) ) : ''; ?>
    286340                                                <?php the_content(); ?>
    287341                                        </div>
    288                                 </div>
     342                                </div><!-- .wcorg-speaker -->
    289343
    290344                        <?php endwhile; ?>
    291345
     
    293347                <?php
    294348
    295349                wp_reset_postdata();
    296                 $content = ob_get_contents();
    297                 ob_end_clean();
     350
     351                $content = ob_get_clean();
    298352                return $content;
    299353        }
    300354