loop through custom post types with meta data

by graphical_force   Last Updated March 13, 2018 08:08 AM

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>


Answers 1


I'm wondering why in nearly every theme I have seen the developers use multiple times the:

get_the_ID()

...function within:

while ( $loop->have_posts() )

...loops.

In my opinion the returned ID is the same (while one iteration of while). In the code above the function get_the_ID is called six times and get_the_ID calls the get_post function which is doing a lot of stuff. This overhead cost performance or memory, or not?

BenK
BenK
March 13, 2018 07:50 AM

Related Questions


Query sticky posts with thumbnails

Updated July 16, 2015 16:03 PM

Getting wrong output after looping through WP_Query

Updated April 03, 2017 11:08 AM

update_post_meta() not updating

Updated February 05, 2018 21:08 PM

Exclude Posts Using Meta Query and User Meta

Updated January 15, 2018 17:08 PM

Sort loop by custom field from different post type

Updated November 13, 2017 16:08 PM