• I have a custom theme that uses a custom post type and multiple custom meta fields.
    Everything is working great but I would like to see if there is a better way to consolidate the queries. I just do not want to be repeating things or doing things that are unnecessary. I have research for weeks and I think I am just confusing myself even more.

    Here is the code the I am using for on the main page to display the most recent post:

    <?php $args = array('post_type' => array( 'post', 'obituaries' ));
    				$loop = new WP_Query( $args );
    				while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
      <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
     	<div class="obits_list"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'thumbnailxs'); ?></a>
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <p><?php echo get_post_meta( get_the_ID(), 'bldob_bdate', true ); ?> ~ <?php echo get_post_meta( get_the_ID(), 'bldob_ddate', true ); ?></p>
        <p>Services will be held: <?php echo get_post_meta( get_the_ID(), 'blser_date', true ); ?></p>
    	</div>
                    </div><!-- #post-<?php the_ID(); ?> -->
    
    		<?php endwhile; ?>

    Any suggestions are greatly appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Are you talking about merging them into an array?

    Thread Starter tmw27529

    (@tmw27529)

    Maybe…
    I was thinking that an array was more for sorting (like post type, taxonomy, number of post to show).

    The site I am working on is for a funeral home. The custom post type (obituaries) admin post input page has multiple custom meta fields for things like the birth dates, date of death and then the dates of the services and locations, etc.
    I want to make sure I am efficiently requesting all of these different fields to display them in the list on the main page and in a sidebar feed.
    Here is a link to the obits page if you want to take a peek Obits

    I fear I might be repeating the request when it is unnecessary.
    I did and still continue to research my curiosity question to know better in the future. But I feel like I am running around in circles without clarification on how to best accomplish this. I hope I am close, or headed in the right direction.
    Any reassurance or guidance would be greatly appreciated. I am such an advocate for WP here at my work and I want to be able to produce great, efficient things correctly.
    Thank you for your time so far!

    Thread Starter tmw27529

    (@tmw27529)

    I feel like this section is repeating request. Can you do a get_post_meta only once? Divide it somehow to include the html and still retrieve it all?

    <p><?php echo get_post_meta( get_the_ID(), 'bldob_bdate', true ); ?> ~ <?php echo get_post_meta( get_the_ID(), 'bldob_ddate', true ); ?></p>
        <p>Services will be held: <?php echo get_post_meta( get_the_ID(), 'blser_date', true ); ?></p>
    Thread Starter tmw27529

    (@tmw27529)

    This is where I am at so far (making a little improvement).
    This appears to slim down things but the html is gone.

    $meta_keys = array('bldob_bdate','bldob_ddate','blser_date');
    foreach ($meta_keys as $key) {
            $key_value = get_post_meta($post->ID, $key, true);
            if (!empty($key_value)) echo ''.$key_value;
    }
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Consolidating Queries’ is closed to new replies.