Tagged: 

Viewing 18 posts - 1 through 18 (of 18 total)
  • Author
    Posts
  • #825116

    Hello,

    I’m using the content element “Blog Posts” and I’d like to filter blog posts according to the year they’ve been posted. The idea is to have a page displaying the posts published within the year and a page “archives” displaying all the posts from previous years.

    I’d like it to be automatic so I do not have anything to do each year. I tried to play with the file blog.php (in avia-shortcodes) so I could choose from there to display posts from a custom taxonomy and then pick the years I want. But I can’t figure it out.

    For the moment, my posts have a category “2017” or “2016” and they are selected based on that. But it isn’t the best solution (it means the authors have to remember to add the category for each post they write).

    Could you help me with this?

    Thank you!

    #828557

    Hey Codettes,

    You`ll need to hire a freelance to accomplish it.

    Best regards,
    John Torvik

    #829730

    Hi John,

    Thank you for your reply. Actually, I wanted to make sure the changes had to be done in the blog.php file.

    I am a web developer, just not familiar with your theme and the way it works. I have paid for extra support to contact you so could you help me with this?

    Thank you!

    #831469

    Hi,

    Thank you for using our theme.

    You have to do the changes in that file (enfold\config-templatebuilder\avia-shortcodes\blog.php), if you are using the ALB element ‘Blog Posts’.

    Check function query_entries to adopt your query.

    Hope, this will help you.

    Best regards,
    Günter

    #835885

    Hi Günter,

    Thank you for your reply. Once the changes have been done, where should I place the file knowing that I use a child-theme?
    Should it be wp-content/theme/child-theme/blog.php,
    wp-content/theme/child-theme/avia-shortcodes/blog.php
    or child-theme\config-templatebuilder\avia-shortcodes\blog.php ?

    Thank you!

    Best regards,

    Anne-Laure

    #835901
    #835943

    Hi Yigit,

    Thanks! It helped a lot!
    I wrote the code and it is partly working but I guess there is something missing (the first part works fine but it doesn’t display the year below once the option is chosen). Could you tell me where I’m wrong?

    			function popup_elements()
    			{
    				$this->elements = array(
    
                        array(	"name" 		=> __("Do you want to display blog posts?", 'avia_framework' ),
                            "desc" 		=> __("Do you want to display blog posts or entries from a custom taxonomy?", 'avia_framework' ),
                            "id" 		=> "blog_type",
                            "type" 		=> "select",
                            "std" 	=> "posts",
                            "subtype" => array( __('Display blog posts', 'avia_framework') =>'posts',
                                                __('Display entries from a custom taxonomy', 'avia_framework') =>'taxonomy',
                                                __('Display entries from a custom date', 'avia_framework') => 'year')),
    
    					array(	"name" 		=> __("Which categories should be used for the blog?", 'avia_framework' ),
    							"desc" 		=> __("You can select multiple categories here. The Page will then show posts from only those categories.", 'avia_framework' ),
    				            "id" 		=> "categories",
    				            "type" 		=> "select",
    	        				"multiple"	=> 6,
                                "required" 	=> array('blog_type', 'equals', 'posts'),
    				            "subtype" 	=> "cat"),
    
                        array(
                            "name" 	=> __("Which Entries?", 'avia_framework' ),
                            "desc" 	=> __("Select which entries should be displayed by selecting a taxonomy", 'avia_framework' ),
                            "id" 	=> "link",
                            "fetchTMPL"	=> true,
                            "type" 	=> "linkpicker",
                            "subtype"  => array( __('Display Entries from:',  'avia_framework' )=>'taxonomy'),
                            "multiple"	=> 6,
                            "required" 	=> array('blog_type', 'equals', 'taxonomy'),
                            "std" 	=> "category"
                        ),
    
                        array(
                            "name"  => __("Which Year?", 'avia_framework' ),
                            "desc"  => __("Select which year should be displayed", 'avia_framework' ),
                            "id"  => "year",
                            "type"  => "select",
                            "multiple"  => 6,
                            "required"  => array('blog_type', 'equals', 'year'),
                        ),

    Thanks a lot!

    Cheers,

    Anne-Laure

    #836244

    Hi,

    Glad we could help you.

    In the code above the content of the select box is missing, should be something like:

    
    
                        array(
                            "name"  => __("Which Year?", 'avia_framework' ),
                            "desc"  => __("Select which year should be displayed", 'avia_framework' ),
                            "id"  => "year",
                            "type"  => "select",
                            "multiple"  => 6,
                            "subtype" => array( __('2015', 'avia_framework') =>'2015',
                                                __('2016', 'avia_framework') =>'2016'
                                              ),
                            "required"  => array('blog_type', 'equals', 'year'),
                        ),
    
    
    

    Best regards,
    Günter

    #836422

    Hi Günter,

    It works fine now. Thanks!
    One more thing – if I may – I should define ‘year’ somewhere so the system knows what are ‘2015’ and ‘2016’ (and so it doesn’t considerate those as strings to display).
    I would write something like that:
    'year' => get_the_date('Y')
    But I don’t see where and not sure how to define ‘year’ in the file. Would it be below the following:

    			function query_entries($params)
    			{
    				global $avia_config;
    				$query = array();
    
    				if(!empty($params['categories']) && is_string($params['categories']))
    				{
    					//get the categories
    					$terms 	= explode(',', $params['categories']);
    				}

    ?

    Thank you very much!

    Kind regards,

    Anne-Laure

    #836534

    Hi,

    Normally PHP makes type conversions automatically where necessary. And in the database query it is converted to a string anyway.

    Best regards,
    Günter

    #836853

    Hi Günter,

    Thanks for the explanation!
    The issue I have is that I can now select the year. But if I select ‘2016’, I see all published posts. I’m missing something somewhere, but I have no clue what. Do you have any idea?

    Thank you very much!

    Kind regards,

    Anne-Laure

    #836972

    Hi,

    In file enfold\config-templatebuilder\avia-shortcodes\blog.php around line 442 you find:

    
    				//if we find categories perform complex query, otherwise simple one
    				if(isset($terms[0]) && !empty($terms[0]) && !is_null($terms[0]) && $terms[0] != "null" && !empty($params['taxonomy']))
    				{
    					$query = array(	'paged' 	=> $page,
    									'posts_per_page' => $params['items'],
                                        'offset' => $params['offset'],
                                        'post__not_in' => (!empty($no_duplicates)) ? $avia_config['posts_on_current_page'] : array(),
                                        'post_type' => $params['post_type'],
    									'tax_query' => array( 	array( 	'taxonomy' 	=> $params['taxonomy'],
    																	'field' 	=> 'id',
    																	'terms' 	=> $terms,
    																	'operator' 	=> 'IN'))
    																	);
    				}
                    else
    				{
    					$query = array(	'paged'=> $page,
                                        'posts_per_page' => $params['items'],
                                        'offset' => $params['offset'],
                                        'post__not_in' => (!empty($no_duplicates)) ? $avia_config['posts_on_current_page'] : array(),
                                        'post_type' => $params['post_type']);
    				}
    
    

    You have to add a date query:

    
    'date_query' => array(	array(	'year'  => $params['year'] ))
    

    that means replace above with:

    
    				//if we find categories perform complex query, otherwise simple one
    				if(isset($terms[0]) && !empty($terms[0]) && !is_null($terms[0]) && $terms[0] != "null" && !empty($params['taxonomy']))
    				{
    					$query = array(	'paged' 	=> $page,
    									'posts_per_page' => $params['items'],
                                        'offset' => $params['offset'],
                                        'post__not_in' => (!empty($no_duplicates)) ? $avia_config['posts_on_current_page'] : array(),
                                        'post_type' => $params['post_type'],
    									'date_query' => array(	array(	'year'  => $params['year'] )),
    									'tax_query' => array( 	array( 	'taxonomy' 	=> $params['taxonomy'],
    																	'field' 	=> 'id',
    																	'terms' 	=> $terms,
    																	'operator' 	=> 'IN'))
    																	);
    				}
                    else
    				{
    					$query = array(	'paged'=> $page,
                                        'posts_per_page' => $params['items'],
                                        'offset' => $params['offset'],
    									'date_query' => array(	array(	'year'  => $params['year'] )),
                                        'post__not_in' => (!empty($no_duplicates)) ? $avia_config['posts_on_current_page'] : array(),
                                        'post_type' => $params['post_type']);
    				}
    
    
    

    And in line 335 you have to add ‘year’ to the default array:

    
    				$atts = shortcode_atts(array('blog_style'	=> '',
    											 'columns' 		=> 3,
                                                 'year'    => 2016,
    
    

    Best regards,
    Günter

    • This reply was modified 7 years, 3 months ago by Günter.
    #836997

    Hi Günter,

    Thank you for this!
    Unfortunately, it’s still showing all of the published posts.
    When you define ‘year’ as being 2016, shouldn’t it limit the year to this particular one? In the example you mentioned where year might be 2015 or 2016, how does it knows what to do if I select 2015?
    In line 335, I tried to add

    				$atts = shortcode_atts(array('blog_style'	=> '',
    											               'columns' 		=> 3,
                                         'year'    => get_the_date('Y'),
                                         'blog_type'    => 'posts',
    			                               'items' 		=> '16',
    			                               'paginate' 	=> 'yes',
    			                               'categories' 	=> '',
    			                               'preview_mode' => 'auto',
    											               'image_size' => 'portfolio',
    			                               'taxonomy'		=> 'category',
    			                               'post_type'=> get_post_types(),
                                         'contents'     => 'excerpt',
    			                               'content_length' => 'content',
                                         'offset' => '0',
                                         'conditional' => ''
    			                                 ), $atts, $this->config['shortcode']);

    But nothing is working… :-/

    Thank you very much again!

    Kind regards,

    Anne-Laure

    #837365

    Hi,

    Can you give me FTP access and a WP admin account to your server, so I can update your file directly? Would be the easiest way.

    You can post it in private content area.

    Best regards,
    Günter

    #837401
    This reply has been marked as private.
    #837487

    Hi,

    I found a solution.

    I updated the file blog.php and postslider.php (both are in the child theme).

    The only restriction by WordPress is, that you can select one year only for filter (or all) – this is the restriction for the date query.

    I hope, this helps you.

    Best regards,
    Günter

    #837499

    Hi Günter,

    You solved it!
    A HUGE thank you!!!

    You rock!

    Have a wonderful weekend!

    Anne-Laure

    #837504

    Hi,

    Glad we could help you.

    If you have time can you place a rating for the support on themeforest please.

    Have a nice weekend also and enjoy the theme.

    Feel free to come back, when have further problems or questions.

    Best regards,
    Günter

Viewing 18 posts - 1 through 18 (of 18 total)
  • The topic ‘How to display blog posts from a custom year automatically ?’ is closed to new replies.