WordPress.org

Ready to get started?Download WordPress

Forums

Show recent post from 2 difrnt custom post catigory (8 posts)

  1. sdesigns
    Member
    Posted 1 year ago #

    I am trying to show the most recent post from two difrnt custom posts catigories. Below is the code that I am trying to use but it is not showing anything. Does anyone have an Idea of what I did wrong. i think it is somthing simple that I am missing thank you.

    <?php
          $wp_query = new WP_Query(array('showposts' => 1, 'post_type' => array('outtakes&outtake_category=outdoor-recreation', 'adirondack_almanac&adirondack_almanac_category=outdoor-recreation')));
    while( have_posts() ) :
        the_post();?>
    
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php
    if ( has_post_thumbnail()):
        the_post_thumbnail( 'feed-fixed' );
    endif;  ?></a>
        <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    <?php echo wp_trim_words( get_the_excerpt(), 20 ); ?>
     <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">>>More</a>
    
    <?php endwhile; ?>
  2. esmi
    Forum Moderator
    Posted 1 year ago #

    <?php
    $wp_query = new WP_Query(array('showposts' => 1, 'post_type' => array('outtakes&outtake_category=outdoor-recreation', 'adirondack_almanac&adirondack_almanac_category=outdoor-recreation')));
    while( $wp_query->have_posts() ) :
    $wp_query->the_post();?>

    http://codex.wordpress.org/Function_Reference/WP_Query#Usage

  3. sdesigns
    Member
    Posted 1 year ago #

    Thank you for that info but it sitll is not working. Bellow is the full code I am using for this. When I use this I get nothing.

    <?php
    $wp_query = new WP_Query(array('showposts' => 1, 'post_type' => array('outtakes&outtake_category=outdoor-recreation','adirondack_almanac&adirondack_almanac_category=outdoor-recreation')));
    while( $wp_query->have_posts() ) :
    $wp_query->the_post();?>
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php
    if ( has_post_thumbnail()):
        the_post_thumbnail( 'feed-fixed' );
    endif;  ?></a>
        <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    <?php echo wp_trim_words( get_the_excerpt(), 20 ); ?>
     <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">>>More</a>
    
    <?php endwhile; ?>

    If I use just the below code I get the most recent from the two difrnt post types but I want to get the most recent post from these two post types but withing the custom post typs catigory of Outdoor-Recreation.

    <?php
          $recentPosts = new WP_Query(array('showposts' => 1, 'post_type' => array('outtakes','adirondack_almanac')));
    while( $recentPosts->have_posts() ) :
        $recentPosts->the_post();?>
    
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php
    if ( has_post_thumbnail()):
        the_post_thumbnail( 'feed-fixed' );
    endif;  ?></a>
        <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    <?php echo wp_trim_words( get_the_excerpt(), 20 ); ?>
     <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">>>More</a>
    
    <?php endwhile; ?>

    Thank you for your help

  4. esmi
    Forum Moderator
    Posted 1 year ago #

  5. sdesigns
    Member
    Posted 1 year ago #

    Thank you I have come up with a new code that is working great except for I cant get it to just show the one most recent post from the two custom post catigories it is showing all the posts.

    Thank you

    <?php
     $myquery['tax_query'] = array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'adirondack_almanac_category',
            'terms' => array('outdoor-recreation'),
            'field' => 'slug',
        ),
        array(
            'taxonomy' => 'outtake_category',
            'terms' => array('outdoor-recreation'),
            'field' => 'slug',
        ),
    );
    query_posts($myquery);
    
     while( $wp_query->have_posts() ) :
    $wp_query->the_post();
    
     ?>
  6. bcworkz
    Member
    Posted 1 year ago #

    Function_Reference/WP_Query#Pagination_Parameters
    By limiting the posts per page and not having page links on your template, the effect is limiting the total post number.

    To ensure you get one post from each category, I believe you will need to do two separate queries, one for each category. Don't forget to wp_reset_postdata() after each query.

  7. sdesigns
    Member
    Posted 1 year ago #

    Thank you I have figured out how to show the post from 3 of my post types catigories but I cant get pagination to work with it. When I click on page 2 it goes to page two but shows the same posts as on page one. Thank you for any help.

    <?php
    
    $myquery['tax_query'] = array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'terms' => array('outdoor-recreation'),
            'field' => 'slug',
        ),
        array(
            'taxonomy' => 'outtake_category',
            'terms' => array('outdoor-recreation'),
            'field' => 'slug',
        ),
    	array(
            'taxonomy' => 'story_category',
            'terms' => array('outdoor-recreation'),
            'field' => 'slug',
        ),
    
    );
    query_posts($myquery);
    
    ?>
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    		<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            							<div class="metadate"><?php the_time('l, F j, Y') ?></div>
    
    <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    
    <div id="featuredimage">
    
    <?php
    if ( has_post_thumbnail()) {
      echo '<a href="' . get_permalink($post->ID) . '" >';
      the_post_thumbnail();
      echo '</a>';
    }
    ?><!-- end featured image --></div>
    					<div class="entry">
                        <?php the_excerpt(); ?>
    
    					</div>
                                     <br class="clearfloat" />
    
    				</div>
    
    	<?php endwhile; ?>
    
    <?php wp_pagenavi(); ?>
    
    	<?php else : ?>
    
    		<h2>Not Found</h2>
    
    	<?php endif; ?>
  8. bcworkz
    Member
    Posted 1 year ago #

    When you use query_posts(), you discard the main query, including the pagination query variables, and replace it with your own, without pagination arguments. You either need to manage the pagination with your own code, or discard the query_posts() approach and use the 'pre_get_posts' filter to set your 'tax_query' query variable.

Topic Closed

This topic has been closed to new replies.

About this Topic