Date Base Archive On WordPress Homepage

Sorry, I am pretty new to WordPress coding and learning it from scratch. Please pardon me for my lack of experience.

I have a custom post type called ‘Products’ and want to display only them as a date based archive in homepage. But, I Don’t want to display the post titles or any other content from the posts except the featured from first three or four posts in that date. Something like this:

Read More

This is what I want to do

I am trying the following code, but it returns the posts as the normal loop.

    <?php $query = new WP_Query( array('post_type' => 'products', 'orderby' => 'date') ); ?>
    <?php if ( $query->have_posts() ) : ?>

        <?php /* Start the Loop */ ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>

            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <?php 
                $archive_year  = get_the_time('Y'); 
                $archive_month = get_the_time('m'); 
                $archive_day   = get_the_time('d'); 
            ?>
            <div class="idpostdate">
                <a href="<?php echo get_day_link( $archive_year, $archive_month, $archive_day); ?>"><?php the_date( 'F j, Y', '', '<span class="datetext">: Click Here To View Products</span>', true );?></a>
            </div>
            <div class="thumbnail">
                <?php if ( has_post_thumbnail() ) { the_post_thumbnail('homet'); } ?>

            </article>

        <?php endwhile; ?>


    <?php else : ?>

        <?php get_template_part( 'no-results', 'index' ); ?>

    <?php endif; ?>

Any guidance?

Related posts

Leave a Reply

1 comment

  1. Functions like the_ID() and the_post_thumbnail() run on the main query. If you want to use their equivalents in your code, you’ll need to prepend $query-> to them.

    Untested, but I think it’ll do what you want it to:

    <?php $query = new WP_Query( array('post_type' => 'products', 'orderby' => 'date') ); ?>
    <?php if ( $query->have_posts() ) : ?>
    
        <?php /* Start the Loop */ ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    
            <article id="post-<?php $query->the_ID(); ?>" <?php $query->post_class(); ?>>
            <?php 
                $archive_year  = $query->get_the_time('Y');
                $archive_month = $query->get_the_time('m');
                $archive_day   = $query->get_the_time('d');
            ?>
            <div class="idpostdate">
                <a href="<?php echo get_day_link( $archive_year, $archive_month, $archive_day); ?>"><?php $query->the_date( 'F j, Y', '', '<span class="datetext">: Click Here To View Products</span>', true );?></a>
            </div>
            <div class="thumbnail">
                <?php if ( $query->has_post_thumbnail() ) { $query->the_post_thumbnail('homet'); } ?>
    
            </article>
    
        <?php endwhile; ?>
    
    
    <?php else : ?>
    
        <?php get_template_part( 'no-results', 'index' ); ?>
    
    <?php endif; ?>