Tagged: ,

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1243991

    I have two pages (none of them is set as blog page in the theme settings): both show posts by using the content element “blog posts”. The differences between the two pages should be based on a custom field (made with ACF) = date.
    The idea is to show in page 1 posts that have the ACF date older than actual date for a time span of maximum 2 years (so as today posts up to 2018). In page 2 I should show posts that have the ACF date older than 2018.

    Consider that the blog posts graphic layout has been modified: for page 1 only via css, and for page 2 by adding new code and conditional if (is_page(204)) to the avia shortcode ” postslider.php from in 766-789 to hide taxonomies and keep only categories.

    I saw that there is a filter by date in blog post content element, but date should be changed manually. I would like to modify that code in some way by using timestamp as reference point to add an automatic calculation. Something like e.g.
    page 1:
    start date = (timestamp – 2 years)
    end date = timestamp

    page 2:
    start date = 2010
    end date = timestamp – (2 years – 1 day)
    Do you have any suggestion? Thanks

    • This topic was modified 4 years, 2 months ago by elenapoliti.
    #1244600

    Hi, have you any suggestion for the question above? Thanks very much

    #1244937

    Hi,

    Thank you for the inquiry.

    We might be able to use the avia_post_slide_query filter to adjust the default date_query of the post slider or blog posts element. An example of the date_query value might look like this:

    array (
      0 => 
      array (
        'after' => 
        array (
          'year' => '2010',
          'month' => '09',
          'day' => '01',
        ),
        'inclusive' => true,
        'before' => 
        array (
          'year' => '2020',
          'month' => '09',
          'day' => '06',
        ),
      ),
    )
    

    This will display posts from September 01, 2010 up to September 09, 2020. To learn more about the date parameter, please check this thread.

    // https://developer.wordpress.org/reference/classes/wp_query/#date-parameters

    Best regards,
    Ismael

    #1244967

    Thanks Ismael. Just one question: in order to pass the new array as a WP query where the array is built as you suggest $date_query = your_array();
    should I use $date_query = AviaHelper::add_date_query( $date_query);
    or should I use $query = new WP_Query( $date_query ); ?

    And, if I have a custom field named “year” in which I actually write the year of the post (not the year when the post is published) can I still use the
    $query = new WP_Query( $args ); within the postslider.php? Because maybe I can simply use the custom field to filter the posts

    Thanks

    #1245608

    Hi,

    What is the value of the ACF or custom field? It might not be necessary to use the add_date_query function or create a new query because the query including the date_query is already available when we use the avia_post_slide_query filter. We just have to do the necessary adjustments for the date_query array based on the value of the ACF field.

    Usage example of the filter can be found here:

    // https://kriesi.at/support/topic/custom-archive-php/#post-849668
    // https://kriesi.at/support/topic/randomize-post-slider-but/#post-1139638
    // https://kriesi.at/support/topic/portfolio-items-showing-in-blog-posts-content-element-on-blog-page/#post-1200512

    Best regards,
    Ismael

    #1245704

    Thanks Ismael, but I am still a bit confused. My custom field is a simple year, which appears on each post (ex. 2018, 2020 etc.). I should organize my blog posts on the two pages according to that field. On one page there should be automatically all posts that have a year comprised between the present (hence getting a timestamp like $today = getdate(); to allow a calculation) and maximum 2 years past , and the other page should contain all the posts up to 2 years ago (always according to the custom field and not the post date publication).
    Something like the following

    $today= getdate();
    $today_year = $today['year'];
    $two_years_ago = $today_year - 2;
    if (is_page (204)) {
      if ($today_year >=$custom_field_value >= ($today_year  - $two_years_ago)) 
      {show the posts}
    }
    else if (is_page (304)) {
     if ($custom_field_value <($today_year  - $two_years_ago) {show the posts}
    }

    My difficulty is:
    1) how to filter custom field value
    2) how to show the posts that satisfy the request

    • This reply was modified 4 years, 2 months ago by elenapoliti.
    #1245782

    Hi Ismael, I got it. The code I used is as follows (I put it in the functions.php). However I got into a new problem:

    My two pages, have the content element ‘blog posts’ where I show all the categories. Programatically by the below code the two pages show different posts according to the acf field = year (anno)

    If however I click on the category link (hence filter by category) the category archive is mess up. I checked and something in the function below enters in conflict with the category archive. Do you have any suggestion?

    I copy in the private section the link to the test page

    add_filter('avia_post_slide_query', 'avia_post_slide_query_mod',10, 2);
    function avia_post_slide_query_mod($query, $params) {
    	$posts = get_posts($query);
    	$today_year = date('yy');
    	$two_years_ago = $today_year - 2;
    	$current = array();
    		foreach( $posts as $post ) {
    			$meta = get_post_meta($post->ID); //acf fields are saved in post meta
    			$acf_year = $meta['anno'][0]; //get the value of the field needed (anno=year)
    			if (is_page(112)){
    				if($acf_year >= $two_years_ago & $acf_year <= $today_year) {
    				$current[] = $post->ID;
    				}	
    			}
    			else if (is_page(204))	{
    				 if($acf_year < $two_years_ago) {
    				$current[] = $post->ID;
    				}	
    			}
    		}
    	$query['post__in'] = $current;
    	return $query;
    }
    • This reply was modified 4 years, 2 months ago by elenapoliti.
    #1246146

    Hi,

    If you click on the category (“tipo 1” or “tipo 2”) in page id= 204 you will see what I mean in messing up the archive. If I cancel the function above, then the archive page works again!

    Since the archive pages are also set to use the grid layout, we have to add a check or condition in the filter above so that we don’t alter the default archive query. We could use the is_archive function like so..

    if( is_archive() ) return $query;
    

    This will return the default post grid query on archive pages. Hope that helps.

    Best regards,
    Ismael

    #1246215

    Thanks it worked! I included the condition if(is_archive()) as follows, and everything is back to normal

    add_filter('avia_post_slide_query', 'avia_post_slide_query_mod',10, 2);
    function avia_post_slide_query_mod($query, $params) {
    	$posts = get_posts($query);
    	$today_year = date('yy');
    	$two_years_ago = $today_year - 2;
    	$current = array();
    
    		foreach( $posts as $post ) {
    			$meta = get_post_meta($post->ID);
    			$acf_year = $meta['anno'][0];
    			
    			if (is_page(112)){
    				if($acf_year >= $two_years_ago & $acf_year <= $today_year) {
    				$current[] = $post->ID;
    				}	
    			}
    			else if (is_page(204))	{
    				if($acf_year < $two_years_ago) {
    				$current[] = $post->ID;
    				}	
    			}
    			else if (is_archive()) {
    				return $query;
    			}
    		}
    
    	$query['post__in'] = $current;
    
    	return $query;
    }

    You can close the ticket. Thanks

    #1246755

    Hi,

    You’re welcome! We’re glad it worked. Please don’t hesitate to open a new thread if you need anything else.

    Have a nice day.

    Best regards,
    Ismael

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘How to set a calculated date to filter blog posts’ is closed to new replies.