Display Gallery Results Based on Current Year and previous year using CPT

1) create custom post type function for gallery

function ab_gallery_post_type() {
  
// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Gallery', 'Post Type General Name', 'ab' ),
        'singular_name'       => _x( 'Gallery', 'Post Type Singular Name', 'ab' ),
        'menu_name'           => __( 'Gallery', 'ab' ),
        'parent_item_colon'   => __( 'Parent Gallery', 'ab' ),
        'all_items'           => __( 'All Gallery', 'ab' ),
        'view_item'           => __( 'View Gallery', 'ab' ),
        'add_new_item'        => __( 'Add New Gallery', 'ab' ),
        'add_new'             => __( 'Add New', 'ab' ),
        'edit_item'           => __( 'Edit Gallery', 'ab' ),
        'update_item'         => __( 'Update Gallery', 'ab' ),
        'search_items'        => __( 'Search Gallery', 'ab' ),
        'not_found'           => __( 'Not Found', 'ab' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'ab' ),
    );
      
// Set other options for Custom Post Type
      
    $args = array(
        'label'               => __( 'Gallery', 'ab' ),
        'description'         => __( 'CPT for gallery function', 'ab' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions'),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
        'show_in_rest' => true,
  
    );
      
    // Registering your Custom Post Type
    register_post_type( 'gallery', $args );
  
}
  
/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/
  
add_action( 'init', 'ab_gallery_post_type', 0 );


// ths code for remove archive title prefix
add_filter('get_the_archive_title_prefix','__return_false');

2) Gallery page (page-gallery.php) (only show current year post)

<?php get_header(); 

if ( have_posts() ) : ?>

<div id="page-<?php the_ID(); ?>" <?php post_class('your-class py-120'); ?>>
   <div class="container">
         <div class="row">
            
            <div class="col-xl-8 col-lg-8">
               	<?php
               	$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE  post_status = 'publish' AND post_type = 'gallery' ORDER BY post_date ASC");
               	$currentYear = date('Y');
               	foreach($years as $year) : if($currentYear == $year){?>
                  <div class="row">
                  		<h1 class="mb-3"><a href="<?php echo get_year_link($year ); ?>?post_type=gallery"><?php echo $year; ?></a></h1>

                  		<?php 
                  		$months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND YEAR(post_date) = '".$year."' AND post_type = 'gallery' ORDER BY post_date ASC");
               		foreach($months as $month) : ?>
               		    <div class="col-md-6">
               			  	<div class="blog-item">
               	               <div class="blog-item-info">
               	                  <h4 class="blog-title">
               	                  	<a href="<?php echo get_month_link($year, $month); ?>?post_type=gallery"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>
               	                  </h4>
               	               </div>
               	            </div>
               	        </div>
                       <?php endforeach; ?>
                   </div>
                   <?php } endforeach; ?>
            </div>

            <!-- for sidebar -->
            <?php get_sidebar('gallery'); ?>

         </div>
   </div>
</div>
	 
<?php endif; 
get_footer(); ?>

3) now we need to create a sirebar for gallery pages (sidebar-gallery.php)

<div class="col-xl-4 col-lg-4">
   <div class="gallery-sidebar">
      <div class="widget">
         <h4 class="widget-title">Years</h4>
         <div class="years-list">
            <?php wp_get_archives( array( 'type' => 'yearly', 'post_type' => 'movies') ); ?>
         </div>
      </div>
   </div>
</div>

4) Gallery archive page (archive-gallery.php) (this page use for showing previous year and month post)

<?php get_header(); ?>	

<?php if ( have_posts() ) : ?>

<div id="post-<?php the_ID(); ?>" <?php post_class('page-class py-120'); ?>>
   <div class="container">
         <div class="row">
            
            <div class="col-xl-8 col-lg-8">
            	<?php if ( is_year() ) { ?>
                  <div class="row">
                  	<h1 class="mb-3"><a href=""><?php $year = get_the_archive_title(); echo $year; ?></a></h1>

         		   	<?php $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND YEAR(post_date) = '".$year."' AND post_type = 'gallery' ORDER BY post_date ASC");
         				  foreach($months as $month) : ?>
         				    <div class="col-md-6">
         					  	<div class="blog-item">
         			               <div class="blog-item-info">
         			                  <h4 class="blog-title">
         			                  	<a href="<?php echo get_month_link($year, $month); ?>?post_type=gallery">
         			                  		<?php echo date( 'F', mktime(0, 0, 0, $month) );?>
         			                  	</a>
         			                  </h4>
         			               </div>
         			            </div>
         			        </div>
         		        <?php endforeach; ?>
                  </div>
                <?php } else { ?>
                	
                	<div class="row">
	                  <h1 class="mb-3"><a href=""><?php the_archive_title(); ?></a></h1>
	                  <?php while ( have_posts() ) : the_post(); ?>
	                  <div class="col-md-12">
	   					  	<div class="blog-item">
				               <div class="blog-item-info">
				                  <h4 class="blog-title">
				                  	<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
				                  </h4>
				               </div>
				            </div>
	   			      </div>
	   			   <?php endwhile; ?>
	   			   </div>
                 <?php } ?>

            </div>
            <!-- for sidebar -->
            <?php get_sidebar('gallery'); ?>

         </div>
   </div>
</div>

		 
<?php else : ?>
	<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php get_footer(); ?>

5) Create a details page for view all details (single-gallery.php)

<?php get_header(); ?>	
<?php if ( have_posts() ):  while ( have_posts() ) : the_post(); ?>
<div class="service-single-area py-120">
   <div class="container">
      <div class="service-single-wrapper">
         <div class="row">                  
            <div class="col-xl-8 col-lg-8">
               <div class="service-details">
                  <div class="service-details-img mb-30">
                     <?php the_post_thumbnail( 'full' ); ?>
                  </div>
                  <div class="service-details">
                     <?php the_title('<h1>', '</h1>'); ?>
                     <?php the_content(); ?>                               
                  </div>
               </div>
            </div>

            <!-- for Sidebar -->
            <?php get_sidebar('gallery'); ?>

         </div>
      </div>
   </div>
</div>
<?php endwhile; else :?>
<p>Sorry no gallery matched your criteria.</p>
<?php endif; ?>
<?php get_footer(); ?>

Leave a Reply

Your email address will not be published.