Show text for post with specific date and specific category in WordPress

So I’m trying to show text at the end of each post but from this month only and only in one category. I tried different codes that I found here but none of them really worked or what I needed. Here is example of what I like to do. Hope it make sense.

 if(in_category(tags) && date(from June 2015) )
    {
            ?>
        <p>See related news <a href="#">here</a>.</p>
       <p>Subscribe to our monthly e-newsletter <a href="#">here</a>.</p>

  <?php } ?>

Related posts

2 comments

  1. if i am not wrong by understanding your quest you need to use date_query along with tax_query

    $args = array(
        'post_type'  => 'page', // 'post', 'page' or custom post type 'Name' Like `Products`
        'tax_query' => array(
                                array(
                                    'taxonomy' => 'category_term', // your taxonomy name
                                    'field' => 'slug', // slug or id 
                                    'terms' => $catSlug
                                )
                            ),
        'date_query' => array(
                array(
                    'year'  => date('Y'), // remove if you dont want
                    'month' => date('m'),
                    'day'   => date('d'), // remove if you dont want
                ),
            ),
    );
    $query = new WP_Query( $args );
    
    echo '<pre>';print_r($query->posts);echo '</pre>';
    

    To check in the Category you should use condition :

    get month of every post date and compare it with current month number or hardcode month number like for June use 06 you can check for php date formats here

    For in_category parameters see this

    if(in_category('cat_name') && date('m',strtotime($post->post_date)) == date('m'))
    {
    
    // your code to view post title 
    
    }
    
  2. Check the below code . Here I had used category ‘Uncategorized’ .you can use your tag there.
    hope this will help. add this code in your functions.php

    add_filter( 'the_content', 'check_cat_date' );
    
    function check_cat_date($content){
    
          $date    = '2015-06-01';
          $pub_Date =the_date( 'Y-m-d', '' ,'', $echo=false );
          $pub_Date = (string)$pub_Date; 
    
          $posted_date = strtotime($pub_Date);
          $flag_date   = strtotime($date);
    
    
    if(in_category('Uncategorized')&&($posted_date) >= $flag_date)
        {
    
    
        $append = '<p>See related news <a href="#">here</a>.</p>
                <p>Subscribe to our monthly e-newsletter <a href="#">here</a>.</p>';
        $new_content = $content.$append;
    
        return $new_content;
    
    
    
        }
        else{
            return $content;
        }
    
    }
    

Comments are closed.