I want to show post for whose birthday is today - wordpress

This is the code i have found in deed, please help me as i am new to wordpress
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' => 'biography',
'meta_key' => 'date_of_birth',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_of_birth',
'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.

Related

how display post on subcategory

I want to display posts in the subcategory but don't know what to do. But this code does not show the post in the category but the entire post in the category. Can you help me?
<?php
$query = new WP_Query( array(
'post_type' => 'hotcheck',
'topics__in' => array($cat->term_id),
) );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="entry">
<h2 class="title"><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
Please try below code
$args = array(
'post_type' => 'hotcheck',
'tax_query' => array(
array(
'taxonomy' => 'category', // Add your taxonomy name here
'field' => 'term_id',
'terms' => $cat->term_id,
),
),
);
$query = new WP_Query($args);
Hope its work for you Thanks!

Advanced custom field query in WordPress

I am trying to list all the post while advanced custom field value ispremium=>yes and post issticky in my WordPress site. I have following code in my page. It is listing all the post but not checking ispremium=>yes and issticky post rather showing all the posts.
What's wrong in my code?
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => 'ispremium',
'meta_value' => 'yes'
);
// query
$the_query = new WP_Query( $args and is_sticky());
?>
<?php if($the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('event_thumbnail'); ?>" />
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Try this:
$args = array(
'posts_per_page' => -1,
'post__in' => get_option( 'sticky_posts' ),
'post_type' => 'post',
'meta_key' => 'ispremium',
'meta_value' => 'yes',
'ignore_sticky_posts' => 1,
);
$the_query = new WP_Query( $args );

Display post for a week

Can someone help me how to display the post only for a week? i have this code that works fine in post_date:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 1,
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'after' => '1 week ago'
)
)
);
?>
<ul class="weekly-list">
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
but how to do it in the custom field date. Because my other events is posted a week before the events. Can someone help me?
Thanks.
This Might help you to get solution:
$week = date('W');
$year = date('Y');
$the_query = new WP_Query( 'year=' . $year . '&w=' . $week );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The previous answer was along the good track. Just modify your date_query:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 1,
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ),
),
),
);
?>
<ul class="weekly-list">
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>

Filter posts by month in ACF custom date picker field

I have a date picker ACF custom field where user picks the event date when creating new post. I display all posts on a page and I order them by this event date. What I need is a filter option above the posts where you can pick the month to see only the events happening that month.
Here is my code:
<?
/*
* Order Posts based on Date Picker value
* this example expects the value to be saved in the format: yymmdd (JS) = Ymd (PHP)
*/
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
query_posts(array(
'paged' => $paged,
'posts_per_page'=>12,
'meta_key' => 'event_date', // name of custom field
'orderby' => 'meta_value_num',
'order' => 'DSC',
'pagination' => 'true',
'cat' => '2'
));
?>
<div class="filter">
<a>January</a>
<a>February</a>
...etc...
</div>
<ul>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<div>
<?php
$date = get_field('event_date');
$dateformat = date("F j, Y", strtotime($date));
?>
<p class="date"><?php echo $dateformat; ?> </p>
<h4><?php the_title(); ?></h4>
</div>
</li>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
</ul>
<?php wp_reset_query(); ?>
add_action( 'pre_get_posts', 'upcoming_events_order');
function upcoming_events_order( $query ) {
if( $query->is_post_type_archive('event') ) :
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_query', array( array( 'key' => 'event_date', 'value' => date('Ymd'), 'compare' => '>=', 'type' => 'DATE' ) ) );
$query->set( 'meta_key', 'event_date' );
$query->set( 'meta_value_num', 'DATE');
endif;
};

Show only post of custom category

I have a custom post types called photo gallery. In that post type I have registered a taxonomy called video. Then i have created two category under video called 'personal' & 'commercial'. Now i want to show only the commercial category post to a page section. How can I do that? Here is the code I have tried but not working
<?php
$args = array(
'post_type'=>'photo_gallerys',
'post_status'=>'publish',
'video'=>'commercial',
'posts_per_page'=>-1,
'paged'=>get_query_var('paged')
);
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Try this code hope you'll find your solution
<?php
$tax_post_args = array(
'post_type' => 'your post type name',
'posts_per_page' => 999,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'your taxonomy name',
'field' => 'id',
'terms' => your category id
)
)
);
$tax_post_qry = new WP_Query($tax_post_args);
while($tax_post_qry->have_posts()) :
$tax_post_qry->the_post();
the_title();
endwhile;
?>
if you want to get posts from category slug then use this code in tax_query array
'field' => 'slug',
'terms' => 'your category slug'
use 'cat' => "your_cat_id in your wp query argument.
for example if I want to display post for only commercial category and commercial have category id 21, then I will write query to display post like this:
<?php
$args = array(
'post_type'=>'photo_gallerys',
'post_status'=>'publish',
'video'=>'commercial',
'cat' => 21,
'posts_per_page'=>-1,
'paged'=>get_query_var('paged')
);

Resources