0

Is there a more efficient way of querying for a "fallback" post in case a post doesn't have a meta key? I'm checking for the meta key featured-checkbox and if it doesn't exist, I get the most recent post. But it seems messy.

Is there a way to query for 2 posts in the first $arg and then select them by meta key? Would that be more complex or less?

// Main Loop for the meta key

$args = array( 
            'post_type' => 'post',  
            'post_status' => 'publish',  
            'posts_per_page' => 1,
            'meta_query' => array(
                array(
                    'key' => 'featured-checkbox',
                    'value' => 'yes' ),
            )
    );

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

    <div class="title">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
    </div>                      


    <?php endwhile; else : ?>

            // Fallback Post Loop of latest post

            <?php
                    $args = array( 
                    'post_type' => 'post',  
                    'post_status' => 'publish',  
                    'posts_per_page' => 1,
                    );
                    $query = new WP_Query( $args );
                    if ( $query->have_posts() ) :
                    while ( $query->have_posts() ) : $query->the_post();
                ?>

    <div class="title">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
    </div>      

                        <?php endwhile; else : endif; wp_reset_postdata();?>

            <?php endif; ?>

    <?php wp_reset_postdata();?>
  • That query is going to be slow and very expensive, as it will involve full table scans. If you want to query for all posts that have X or Y, then you should be using a taxonomy to group them ( e.g. a tag called featured, or a term in a custom taxonomy ). Doing that would be orders of magnitude faster than a post meta query. I also implore you to correctly indent your code and not put multiple things on the same line, it makes the code much harder to read, a decent editor will automatically do this for you – Tom J Nowell Dec 16 '16 at 18:37
  • Thanks. For simplicity, I left out the category $arg in the query, so I am narrowing by taxonomy somewhat. I don't want to use a tag, as I don't want a tag category. I really need to use a meta key so the end user can select the featured post in the editor. – BlueDogRanch Dec 16 '16 at 18:50
  • @BlueDogRanch you can use a custom taxonomy if you want it separate, and you are not obligated to use the built in taxonomy interface- you can implement your own interface for selecting a featured post and save it as a tax term association behind the scenes the same way you can save your own meta data. – Milo Dec 16 '16 at 22:08
  • Ok, do you have a link to docs for those tax term associations? – BlueDogRanch Dec 16 '16 at 23:26
  • Selecting the featured post in the editor is just a UI problem, there's nothing stopping you backing your UI with terms not post meta, just use a hidden taxonomy with post IDs as the slug, or better yet a single term called featured-checkbox – Tom J Nowell Dec 17 '16 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.