WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Filter custom post types from "latest posts" (6 posts)

  1. lerrie
    Member
    Posted 1 year ago #

    On my website's static front page I am showing my 3 latests blog posts by:

    <div id="nieuws">
    
    <?php $args = array( 'numberposts' => 3 ); $lastposts = get_posts( $args ); foreach($lastposts as $post) : setup_postdata($post); ?>
    
    <div class="nieuws">
    <div class="thumbnail">
    <?php the_post_thumbnail( 'news-thumbnail' ); ?>
    </div>
    <div class="nieuwstitel">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </div>
    </div>
    <?php endforeach; ?>
    </div>

    Howerver, 2 custom post types also show up. (custom post type 1: slideshow custom post type 2: information) Is there a way to make sure these 2 custom post types don't show up in the "latest post" on my front page? I just want to show the regular blog posts.

    Thanks!

  2. bythegram
    Member
    Posted 1 year ago #

    I think if you add this to your $args array:

    'post_type' => 'post'

    is should fix your issue.
    <?php $args = array( 'numberposts' => 3, 'post_type' => 'post' ); $lastposts = get_posts( $args ); foreach($lastposts as $post) : setup_postdata($post); ?>

  3. lerrie
    Member
    Posted 1 year ago #

    Not working. It should somehow work because when I change the 'post_type' => 'post' to 'post_type' => 'NAME_OF_MY_CUSTOM_POST_TYPE' it does work. Someone else?

  4. Sumit Singh
    Member
    Posted 1 year ago #

    get_posts only pull posts not custom posts.
    it should be work. if doesn't then try wp_query

  5. bythegram
    Member
    Posted 1 year ago #

    For a little more clarification here is how I think you should be able to use wp_query to achieve the same results as you are looking for out of get_posts:

    <?php
    $args = array(
              'posts_per_page' => 3,
              'post_type' => 'post'
            );
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    ?>
    <div class="nieuws">
    <div class="thumbnail">
    <?php the_post_thumbnail( 'news-thumbnail' ); ?>
    </div>
    <div class="nieuwstitel">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </div>
    </div>
    <?php
    endwhile;
    // Reset Post Data
    wp_reset_postdata();
    ?>
  6. lerrie
    Member
    Posted 1 year ago #

    Thanks I fixed it!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.