{"id":3453,"date":"2018-11-01T02:35:48","date_gmt":"2018-11-01T02:35:48","guid":{"rendered":"https:\/\/docs.contentviewspro.com\/?p=3453"},"modified":"2024-03-15T02:30:45","modified_gmt":"2024-03-15T02:30:45","password":"","slug":"list-posts-by-year-display-yearly-archives","status":"publish","type":"docs","link":"https:\/\/contentviewspro.com\/documentation\/article\/list-posts-by-year-display-yearly-archives\/","title":{"rendered":"Frontend filter posts by years (display yearly archives), by published date"},"content":{"rendered":"<p>If you want to show list of years, and list of posts in each year, this document will help.<br \/>\nWe will leverage the live filter for the custom field to display yearly archives, with only 2 steps:<\/p>\n<ul>\n<li>Step 1: add a custom field to every existing and upcoming posts, to store the published year or date.<br \/>\nPlease add this code to file <strong>functions.php<\/strong> in the theme&#8217;s folder (or install this plugin <a href=\"https:\/\/wordpress.org\/plugins\/code-snippets\/\" rel=\"nofollow\">Code Snippets<\/a> then add this code to <a href=\"https:\/\/ps.w.org\/code-snippets\/assets\/screenshot-2.png\" rel=\"nofollow\">the &#8220;Code&#8221; textarea<\/a>):<\/p>\n<pre style=\"font-size: 13px;\">\/\/ Content Views Pro - add custom field to store the published year or date\n$GLOBALS[ 'cvp_theme_lfdpt' ] = 'post'; \/\/ Can change to another post type\n$GLOBALS[ 'cvp_theme_lfdvar' ] = 'cvp_published_year';\n$GLOBALS[ 'cvp_theme_lfdtype' ] = 'year'; <strong>\/** Or change to 'published_date' if you want to filter by published date *\/<\/strong>\n\nadd_action( 'admin_head', 'cvp_theme_add_ctf_dmy_to_exising_posts' );\nfunction cvp_theme_add_ctf_dmy_to_exising_posts() {\n\t$option = 'cvp_theme_add_ctf_year' . $GLOBALS[ 'cvp_theme_lfdpt' ];\n\tif ( !get_option( $option ) ) {\n\t\tglobal $wpdb;\n\t\t$mval = ($GLOBALS[ 'cvp_theme_lfdtype' ] === 'published_date') ? \"DATE_FORMAT(post_date_gmt, '%Y-%m-%d')\" : 'YEAR(post_date_gmt)';\n\t\t$wpdb-&gt;query( \"INSERT INTO {$wpdb-&gt;postmeta} (post_id,meta_key,meta_value)\n  SELECT ID, '{$GLOBALS[ 'cvp_theme_lfdvar' ]}', {$mval}\n  FROM {$wpdb-&gt;posts} WHERE {$wpdb-&gt;posts}.post_type = '{$GLOBALS[ 'cvp_theme_lfdpt' ]}' AND {$wpdb-&gt;posts}.post_status = 'publish'\" );\n\t\tadd_option( $option, 1, '', false );\n\t}\n}\n\nadd_action( 'save_post', 'cvp_theme_add_ctf_dmy_to_new_posts' );\nfunction cvp_theme_add_ctf_dmy_to_new_posts( $post_id ) {\n\tif ( !wp_is_post_revision( $post_id ) &amp;&amp; get_post_type( $post_id ) === $GLOBALS[ 'cvp_theme_lfdpt' ] ) {\n\t\t$val = ($GLOBALS[ 'cvp_theme_lfdtype' ] === 'published_date') ? 'Y-m-d' : 'Y';\n\t\tupdate_post_meta( $post_id, $GLOBALS[ 'cvp_theme_lfdvar' ], get_the_date( $val, $post_id ) );\n\t}\n}\n\/\/ End code\n<\/pre>\n<\/li>\n<li>Step 2: Create a new View to show the years list, and allow visitors to filter posts by year.<br \/>\nIn Admin area, click Content Views &gt; Add New menu item.<br \/>\nIn <strong>Filter Settings<\/strong> tab of the View, select <code>Custom Fields<\/code> in Advance group, then click <code>Add New<\/code> button.<br \/>\nA new field will be added, please select options as same as here:<img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-3835\" src=\"https:\/\/contentviewspro.com\/documentation\/wp-content\/uploads\/2018\/11\/live-filter-by-year.png\" alt=\"Content Views Pro - live filter by year\" width=\"660\" height=\"487\" srcset=\"https:\/\/contentviewspro.com\/documentation\/wp-content\/uploads\/2018\/11\/live-filter-by-year.png 660w, https:\/\/contentviewspro.com\/documentation\/wp-content\/uploads\/2018\/11\/live-filter-by-year-300x221.png 300w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<blockquote><p>If you show frontend filter by <strong>published date<\/strong>, please select the option <strong>Date<\/strong> for &#8220;Value Type&#8221;.<br \/>\nTo show filters from date to date, please select the option <strong>Date Range Picker<\/strong> for &#8220;Type&#8221;.<\/p><\/blockquote>\n<\/li>\n<\/ul>\n<p>Then you can save the View, and add View shortcode to where you want to display the yearly archives.<\/p>\n<p>Best regards,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to show list of years, and list of posts in each year, this document will help. We will leverage the live filter for the custom field to display yearly archives, with only 2 steps: Step 1: add a custom field to every existing and upcoming posts, to store the published year or &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/contentviewspro.com\/documentation\/article\/list-posts-by-year-display-yearly-archives\/\"> <span class=\"screen-reader-text\">Frontend filter posts by years (display yearly archives), by published date<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"doc_category":[55],"doc_tag":[21,35],"class_list":["post-3453","docs","type-docs","status-publish","hentry","doc_category-advanced-modification","doc_tag-custom-code","doc_tag-live-filter"],"year_month":"2026-09","word_count":419,"total_views":"0","reactions":{"happy":"1","normal":"0","sad":"2"},"author_info":{"name":"admin1","author_nicename":"admin1","author_url":"https:\/\/contentviewspro.com\/documentation\/author\/admin1\/"},"doc_category_info":[{"term_name":"Advanced Modification","term_url":"https:\/\/contentviewspro.com\/documentation\/cats\/advanced-modification\/"}],"doc_tag_info":[{"term_name":"custom PHP code","term_url":"https:\/\/contentviewspro.com\/documentation\/tags\/custom-code\/"},{"term_name":"live filter","term_url":"https:\/\/contentviewspro.com\/documentation\/tags\/live-filter\/"}],"_links":{"self":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs\/3453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/comments?post=3453"}],"version-history":[{"count":4,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs\/3453\/revisions"}],"predecessor-version":[{"id":5830,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs\/3453\/revisions\/5830"}],"wp:attachment":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/media?parent=3453"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/doc_category?post=3453"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/doc_tag?post=3453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}