Forums

[resolved] Block visibility depending on the date of communication (6 posts)

  1. Mixa
    Member
    Posted 1 year ago #

    On the main page has a block (which has its own design) with posts. These posts - announcements of events. Naturally, when they are already outdated, they continue to hang if you have not added new announcements ...

    I would like to make an opportunity to ask the date until which the relevant announcements. So that when the announcement has already expired, it will not be displayed. And when there are no recent announcements, it did not appear to block announcements (his design).

    On-the idea was to solve the problem of such a decision:

    function filter_where($where = '') {
        $expDate = date( 'Y-m-d', strtotime( get_post_meta($post->ID, 'aexp', true) ) );
        $where .= " AND '" .$expDate >= date('Y-m-d', strtotime('-1 days')) . "'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
        $args=array(
          'post_type' => 'post',
          'post_status' => 'publish',
          'cat'=>28,
          'posts_per_page' => 2
          );
    $my_query=new WP_Query($args);
    remove_filter('posts_where', 'filter_where');
    
      if( $my_query->have_posts() ) {
        echo '<h2>Number of posts is '.count($my_query->posts) . '</h2>';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><small><?php the_time('d.m.y') ?></small> <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
         <?php
        endwhile;
      } else {echo "Нема постів!";} 
    
    //if ($my_query)
    wp_reset_query();

    But it did not work - posts are displayed, but outside of the dates and custom fields :(

    Maybe someone knows how to implement a similar, but the working pattern?

  2. MichaelH
    Volunteer
    Posted 1 year ago #

    I believe the problem is trying to use $post->ID in that filter.

    Since your expiration date is stored in a custom field (postmeta) then you are better off just query for all category 28 posts, then in your loop just display two posts that meet your expiration date requirements.

  3. Mixa
    Member
    Posted 1 year ago #

    That's how you suggest?

    echo $block_do; // events div design start
    
            $i = 0;
    	query_posts('cat=28&posts_per_page=2);
    	if (have_posts()) :
    		while (have_posts()) :
    			the_post(); ?>
    			<?php $expDate = date( 'Y-m-d', strtotime( get_post_meta($post->ID, 'aexp', true) ) ); ?>
    			<?php if $expDate >= date('Y-m-d', strtotime('-1 days')) { ?>
    
    			<p><small><?php the_time('d.m.y') ?></small> <a>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    
    			<?php } ?>
    		<?php endwhile;
    	endif;
    
    echo $block_posle; // events div design end

    But how then does not display the box design ($ block_do $block_posle), when the actual announcement is not?

  4. MichaelH
    Volunteer
    Posted 1 year ago #

    You would not use the post_per_page in the query. You would need a counter in your loop to test that.

  5. Mixa
    Member
    Posted 1 year ago #

    I do not quite understand the scheme you are offering ...
    Make two loop? And in the first count the availability of posts. A second show has everything?

  6. Mixa
    Member
    Posted 1 year ago #

    All in all figured out. Can someone come in handy ...

    Here's the working code ready

    $i = 0;
    query_posts('cat=28');
    
    if (have_posts()) :
    
    while (have_posts()) :
    the_post(); ?>
    <?php $expDate = date( 'Y-m-d', strtotime( get_post_meta($post->ID, 'aexp', true) ) ); ?>
    <?php if ($expDate >= date('Y-m-d', strtotime('-1 days'))) { ?> 
    
    <?php // работаем пока постов не будет больше 2-х
    if ($i < 2) { ?>
    
    <?php $i++; ?>
    
    <?php
    // запускаем дизайн блока анонсов
    if ($i == 1) { echo $block_do;} ?>
    
    <p><small><?php the_time('d.m.y') ?></small> <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    
    <?php
    
    // конец условия выборки по кол-ву
    }
    // конец условия выборки по кастом-филдз
    }
    
    endwhile;
    
    endif;
    
    wp_reset_query(); //just in case
    
    // закрываем див с дизайном блока анонсов
    if ($i > 0) { echo $block_posle;}

    Thanks to all who help from!

Topic Closed

This topic has been closed to new replies.

About this Topic