Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm really curious as to how might be the quickest way to loop through a custom post type with meta data. I have some code that works but it's slow. I'm using wp_query and it works.

I have also a bunch of conditionals that check whether meta data exists and if so, to do something which is usually display it and a few times display a small image. There are 47 custom post types but maybe I'm just doing too much on one page? I've thought about using ajax to load them (kind of like infinite scrolling) but I'm not sure of the best method.

I was wondering that what is "cost" of looping though custom post types with over 30 different meta data fields? Is there a better way to scan through the tables or is there other methods that don't have the high overhead?

Thanks!

Here is my code and I'll check out those tools as well.

                        <?php $count = 0; ?>
                <?php $args = array( 
                    'post_type' => 'property', 
                    'posts_per_page' => 100, 
                    'orderby' => 'meta_value', 
                    'meta_key' => 'random_775', 
                    'order' => 'ASC'
                    );


                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post(); ?>

                    <div class="<?php wpp_css('property_overview::row_view', "wpp_row_view wpp_property_view_result"); ?>">
                      <div class="<?php wpp_css('property_overview::all_properties', "all-properties"); ?>">

                        <?php $countyCheck = $theCounty; 
                        $theCounty = get_post_meta( get_the_ID(), 'random_775', true );

                        if ($theCounty != $countyCheck ){
                        echo '<h2 class="propTitleh2">' . $theCounty . ' County</h2>';
                        } ?>

                    <a href="<?php the_permalink(); ?>">
                    <div class="<?php wpp_css('property_overview::property_div', "property_div {$property['post_type']} clearfix"); ?>">


                                <ul class="<?php wpp_css('property_overview::data', "wpp_overview_data"); ?>">

                                <?php $sArea = get_post_meta( get_the_ID(), 'area', true );
                                $nArea = preg_replace('/[^0-9]/','',$sArea);
                                $nArea = $nArea / 43560; // convert square feet area into acres
                                $sArea = number_format($nArea,1,'.',',');
                                echo '<div id="overview_main_column">';
                                    echo '<div id="overview_left_column">';
                                        echo '<li><h3>' . get_post_meta( get_the_ID(), 'total_acres', true ) . ' acres +/- for sale</h3></li>';
                                        echo '<div id="propBulletPointsBlock">';
                                            $first_bullet_point_check = get_post_meta( get_the_ID(), 'first_bullet_point', true );
                                            if (isset($first_bullet_point_check[0])) {
                                                $firstPost = $first_bullet_point_check;
                                                $firstPost = substr($firstPost, 0, 30); 
                                                echo '<li><div class="propBulletPoints">&#183; ' . $firstPost . '...</div></li>';
                                            }
                                            $second_bullet_point_check = get_post_meta( get_the_ID(), 'second_bullet_point', true );
                                            if (isset($second_bullet_point_check[0])) {
                                                $secondPost = $second_bullet_point_check;
                                                $secondPost = substr($secondPost, 0, 30);
                                                echo '<li><div class="propBulletPoints">&#183; ' . $secondPost . '...</div></li>';
                                            }
                                            $third_bullet_point_check = get_post_meta( get_the_ID(), 'third_bullet_point', true );
                                            if (isset($third_bullet_point_check[0])) {
                                                $thirdPost = $third_bullet_point_check;
                                                $thirdPost = substr($thirdPost, 0, 30);
                                                echo '<li><div class="propBulletPoints">&#183; ' . $thirdPost . '...</div></li>';
                                            }
                                            $turn_key_check = get_post_meta( get_the_ID(), 'turn_key', true );
                                            if ( $turn_key_check == 'true' ) {
                                                echo '<li><div class="specialFeaturedTag"><img class="reduced-price-overview-img" src="' . get_template_directory_uri() . '/images/turnKeyLabel.png"></div></li>';
                                            }
                                        echo '</div>';
                                    echo '</div>';
                                    echo '<div id="overview_right_column">';
                                        //display image
                                        the_post_thumbnail();
                                        $pending_check = get_post_meta( get_the_ID(), 'pending', true );
                                        if ( $pending_check  == 'true' ) {
                                            echo '<div class="reduced-price-overview">
                                                    <img class="reduced-price-overview-img" src="' . get_template_directory_uri() . '/images/pendingRibbon.png">
                                                    </div>';
                                        }
                                        $sold_check = get_post_meta( get_the_ID(), 'sold', true );
                                        if ( $sold_check == 'true' ) {
                                            echo '<div class="reduced-price-overview">
                                                    <img class="reduced-price-overview-img" src="' . get_template_directory_uri() . '/images/soldRibbon.png">
                                                    </div>';
                                        }
                                    echo '</div>';
                                echo '</div>';
                                $sAddress = get_post_meta( get_the_ID(), 'address', true );

                                            $asAddress = explode(',',$sAddress);
                            if (is_array($asAddress)) { // remove country
                                array_pop($asAddress);
                            }
                            if (is_array($asAddress)) { // remove zip
                                $sStateCode = array_pop($asAddress);
                                $sStateCode = preg_replace('/[^A-Z]*/','',$sStateCode);
                            }
                            $sCounty = get_post_meta( get_the_ID(), 'random_775', true );
                            $sAddress = "$sCounty, $sStateCode";

                            echo '<div class="propertyRight">';
                                echo '<p class="propertyBg">' . get_post_meta( get_the_ID(), 'random_775', true ) . ' Co.</p>'; 
                                echo '<p>PRICE</p>';
                                echo '<div id="mobilePropPrice">' . get_post_meta( get_the_ID(), 'price', true ) . '</div>';
                                echo '<div id="specialFeaturedTagList">';
                                    $tillable_check = get_post_meta( get_the_ID(), 'tillable', true );
                                    if ( $tillable_check == 'true' ) {
                                        echo '<li><div class="specialFeaturedTag"><img class="reduced-price-overview-img" src="' . get_template_directory_uri() . '/images/tillableLabel.png"></div></li>';
                                    }
                                    $tillable_check = get_post_meta( get_the_ID(), 'reduced_price', true );
                                    if ( $tillable_check == 'true' ) {
                                        echo '<li><div class="specialFeaturedTag"><img class="reduced-price-overview-img" src="' . get_template_directory_uri() . '/images/reducedPriceLabel.png"></div></li>';
                                    }
                                echo '</div>';
                            echo '</div>';
                            ?>

                                <?php 
                                $children_check = get_post_meta( get_the_ID(), 'children', true );
                                if($show_children && $children_check): ?>
                                <li class="child_properties">
                                    <div class="wpd_floorplans_title"><?php echo $child_properties_title; ?></div>
                                    <table class="wpp_overview_child_properties_table">
                                        <?php foreach($children_check as $child): ?>
                                        <tr class="property_child_row">
                                            <th class="property_child_title"><a href="<?php echo $child['permalink']; ?>"><?php echo $child['post_title']; ?></a></th>
                                            <td class="property_child_price"><?php echo $child['price']; ?></td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </table>
                                </li>
                                <?php endif; ?>

                                <?php if(!empty($wpp_query['detail_button'])) : ?>
                                <li><a <?php echo $in_new_window; ?> class="button" href="<?php echo $property['permalink']; ?>"><?php echo $wpp_query['detail_button'] ?></a></li>
                                <?php endif; ?>
                           </ul>
share|improve this question
    
Can you share the code you already have? This will help the people here to assess the thing better –  Maruti Mohanty Sep 23 '13 at 7:52
    
The issue with "slow" is that it's often slow for sneakily different things than one assumes it is. The first step should always be profiling the heck out of it (xdebug or xhprof are staples) and determining what precisely is slow. –  Rarst Sep 23 '13 at 9:13
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.