0

I am developing a blog website and it is a biography directory, how can i get posts for specific birth date and can fetch post for today's birth date. for example:on the date 17 may want to Show post for Biography those born on 17 may. and on the date of 18 may Show post for Biography those born on 18 may. I have attached an image and also linked a website with similar featured, i will like to know how to do this. enter image description here http://frostsnow.com/

0

What you can do is create a meta field under a post and store birth date in that field. Now you need meta_query in you post to retrieve the current date posts.

    <?php $currentDate = current_time( 'Y-m-d' );
$args = array(
    'post_type'  => 'my_custom_post_type',
    'meta_key'   => 'birth_date',
    'order'      => 'ASC',
    'meta_query' => array(
        array(
            'key'     => 'birth_date',
            'value'   => $currentDate,
            'compare' => 'IN',
        ),
    ),
);
$query = new WP_Query( $args );
 if ( $query ->have_posts() ) : ?>

    <!-- the loop -->
    <?php while ( $query ->have_posts() ) : $query ->the_post(); ?>
        <h2><?php the_title(); ?></h2>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Note :

1) "birth_date" is the key of the meta field that should be created under the post.

2) Format of current date and stored date should be same.

  • Hi, i tried it but its not working, can you please advice me any other way. – J.Lee Jun 19 '17 at 15:20
  • Use compare LIKE or = . 'compare' => '=', – Waqas Ali Shah Jun 20 '17 at 16:47
  • Thanks its working now, now I have a custom post type named birthday to shows all biography. (Note: eg: For today's date there is 10 biography. I have a section called today's birthday in front-page and have shown 1 biography with its title and thumbnail for today's date, and i want to link all biography for today's birthday date by clicking view all button from home page.) – J.Lee Jun 20 '17 at 17:28
  • Just add post per page in $args 'post_per_page' => 1, – Waqas Ali Shah Jun 21 '17 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.