• jacksouthernweb

    (@jacksouthernweb)


    Ok, so I’ve got a custom taxonomy setup of ‘Writer’ and it’s a way to tie posts to an employee on the site but the client doesn’t want everyone to actually be setup as an author. So, on the employee profile pages I pull the posts with the employees name as the ‘Writer’, which works great. I’ve then got a fallback setup – code below.

    <?php
    // WP_Query arguments
    global $post;
    $slug = get_post( $post )->post_name;
    
    $args = array (
    	'posts_per_page'         => '1',
    	'writer'				 => $slug,
    );
    
    // The Query
    $query = new WP_Query( $args );
    
    // The Loop
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		?>
    			// Do Stuff
    		<?php
    	}
    } else {
    	$fallbackargs = array (
    		'posts_per_page'         => '1',
    		'category'				 => 'featured',
    	);
    
    	$fallback = new WP_Query ( $fallbackargs);
    	while ( $fallback->have_posts() ) {
    		$fallback->the_post();
    		?>
    			// Do Stuff
    		<?php
    	}
    }
    
    // Restore original Post Data
    wp_reset_postdata();
    ?>

    So, basically what I’m asking is this: Dear smarter people of the WordPress community – how can I do this more efficiently, or have I screwed up and done this semi-correctly? Thanks in advance!

The topic ‘WP_Query Fallback’ is closed to new replies.