WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] if 'featured' post exists display it - if not display latest. (3 posts)

  1. surfsup74
    Member
    Posted 1 year ago #

    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?

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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();
  3. surfsup74
    Member
    Posted 1 year ago #

    Perfect. Thanks Alchymyth.

Topic Closed

This topic has been closed to new replies.

About this Topic