Support » Fixing WordPress » if 'featured' post exists display it – if not display latest.

  • Resolved surfsup74

    (@surfsup74)


    I would like a loop that displays a post tagged ‘featured’ if it exists – if, however there is no ‘featured’ post for it to display then I’d like to just display the latest post. How do I do this please?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Michael

    (@alchymyth)

    Forum Moderator

    http://codex.wordpress.org/Class_Reference/WP_Query

    example:

    $my_query = new WP_Query( array( 'posts_per_page' => 1, 'tag' => 'featured' ) );
    
    if( !$my_query->have_posts() ) $my_query = new WP_Query( array( 'posts_per_page' => 1, 'post_type' => 'post' ) );
    
    if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();
    
    /*whatever your output for the post*/
    
    endwhile;
    endif; wp_reset_postdata();

    Perfect. Thanks Alchymyth.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘if 'featured' post exists display it – if not display latest.’ is closed to new replies.