Wordpress Custom Field Query-Date Field - wordpress

I have a custom field 'birthday' which displays in the format (Ymd) .
I want to query posts that have their month(m) field equal to the current month so I can congratulate people throughout the month.
I know that I will have to match the birthdays month part(substring?) to something like $date = date('m');
But am not sure how to go about building the query...Thanks in advance.Cheers

about the query should be like this
$args = array(
'meta_query' => array(
array(
'key' => 'birthdays',
'value' => '$m',
'compare' => 'IN',
),
),
);
query_posts( $args );

Related

WordPress custom query by meta_key

I'm trying to figure out how to achieve a specific query to modify my search results for posts with WordPress. I'm trying to search via a custom field called "Common Authors".
There can be multiple Common authors, which is causing my query to sometimes fail. Here is what I've got for now:
<?php
...
$query->set('meta_key', 'common_authors');
$query->set('meta_value', serialize( array(strval($_GET['common_author'])))); // I get a single ID from the url as a string
$query->set('meta_compare', 'IN');
This is what I see when I var_dump the query:
'meta_key' => string 'common_authors'
'meta_value' => string 'a:1:{i:0;s:5:"17145";}'
This works fine if there is only one common_author.
However, there can be multiple common_authors for a post. Here is a meta_value example from the database:
a:4:{i:0;s:5:"14409";i:1;s:5:"17145";i:2;s:5:"14407";i:3;s:5:"14406";}
Could somebody help me out, to figure out how to adapt my query, so that it would return this one as well?
Try This One Work Prefect!
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'common_authors',
'value' => array ( 'author1', 'author2', 'author3' ),
'compare' => 'IN'
)
)
);
$query = new WP_QUERY($args);
if You Need To Use It In pre_get_posts
$meta_query = array(
array(
'key' => 'common_authors',
'value' => array ( 'author1', 'author2', 'author3' ),
'compare' => 'IN'
)
);
$query->set('meta_query', $meta_query);

Wordpress - display posts in Elementor page using custom field where type is date

I've tried to implement this, while trying to apply some bits of code taken from here and there.
I'm using Elementor Page Builder in a website and I created a custom post type with ACF. Inside that post type, there's a field for a start_date.
The idea is to only show posts in homepage when the date is equal or higher to present date.
Right now the code is like this:
add_action( 'elementor/query/por_data', function( $query ) {
// Here we set the query to display posts
// after specific date
$query->set( 'date_query', array(
array(
'after' => 'May 17, 2020',
)
) );
} );
This obviously just shows posts created after 17 May 2020, which isn't what I want. The idea is to grab the date from that ACF field, compare it with current date and show results accordingly. The custom field type name is "curso"
Found this piece of code but can seem to merge the two together as I have very to little knowledge in programming (yet)
$args = array(
'post_type' => 'events',
'orderby' => 'event_date',
'meta_key' => 'event_date',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => date('Y-m-d',time()),
'compare' => '>'
)
),
);
$q = new WP_Query($args);
var_dump($q->request);
Can someone help? Thanks
I tried to explain it in the comments - hopefully you will get a better understanding of it now.
add_action( 'elementor/query/por_data', function( $query ) {
$meta_query = $query->get( 'meta_query' );
// Append our meta query instead of overwriting all elementors own metaqueries
if($meta_query == ""){
$meta_query = array();
}
$meta_query[] = array(
array(
'key' => 'start_date', //Or whatever you field is called
'value' => date('Y-m-d',time()), //Make sure that the format is correct here
'compare' => '>' //Means that the value should be higher than the key, so this is what you want - If you also need the current dates posts, then you need to use '>=' instead
)
);
$query->set( 'meta_query', $meta_query ); //since we appended our query, you can safely set the meta querie now.
} );
You might need to tweak it a little, but this will lead you the right way.

Compare date values in Wordpress Wp_Query

I’m running a WordPress website with around 35 000 customs posts.
Each post has a birthday value date (format DD/MM/YYYY) stored in a ACF field named birthday
Example :
$birtday_date = get_field("birthday"); //example 02/07/1955
I would like to run 2 queries in order to compare each post birthday value with the 34999 other birthday dates values in my wordpress, count and display the number of people older and younger.
Here is my code but it doesn’t work as I don’t really understand how to compare a value
Can you help me with that ?
<?php
$args = array( // args and query to look for older people that $birthday_date
'posts_per_page' => -1,
'post_type' => 'people',
'meta_key' => 'birthday',
'meta_value' => $birthday_date,
'compare' => '>',
'type' => 'DATE'
);
$query = new WP_Query($args);
$superior = $query->found_posts;
wp_reset_query();
$args2 = array( // args and query to look for younger people that $birthday_date
'posts_per_page' => -1,
'post_type' => 'people',
'meta_key' => 'birthday',
'meta_value' => $birthday_date,
'compare' => '<',
'type' => 'DATE'
);
$query2 = new WP_Query($args2);
$inferior = $query2->found_posts;
wp_reset_query();
echo $superior; // should display the number of older people but display number of people born the same day than $birthday_date
echo $inferior; // should display the number of younger people but display number of people born the same day than $birthday_date
Thanks for your help.
Regards.
You probably only need one actual query here, I'd suggest using just get_posts instead since you're not looping anything. And you can just count the difference from the total number of posts in the post type.
<?php
$birthday_date = get_field("birthday");
// Count the total number of posts in the post type and get the ones that are published. Note this shorthand only works in PHP5
$total_posts = wp_count_posts('people')->publish;
// Use this for PHP4
// $count_posts = wp_count_posts('people');
// $total_posts = $count_posts->publish;
$args = array( // args and query to look for older people that $birthday_date
'posts_per_page' => -1,
'post_type' => 'people',
'meta_query' => array(
'key' => 'birthday',
'value' => $birthday_date,
'compare' => '>',
'type' => 'DATE'
)
);
// Get the posts with the older birthdays
$older_birthdays = get_posts($args);
// Count the number of posts in the older birthdays
$num_of_older_bdays = count($older_birthdays);
// Calculate the other birthdays remaining
$num_of_younger_bdays = $total_posts - $older_birthdays;
// Display your birthday counts
echo $num_of_older_bdays;
echo $num_of_younger_bdays;

WP Query to compare today's day with custom field date for showing birthday's today

I am using ACF's datepicker for custom field to add birthday. THe format is January 7, 2019. I want to show artists having their birthday today. Following is my loop code but I'm stuck with how to compare today's day and month with the value stored in custom field.
<?php
$today = date("F j");
$args2 = array (
'post_type'=> array( 'artist'),
'posts_per_page' => '-1',
'meta_key' => 'artist_birth_date',
'meta_query' => array(
array(
'key' => 'artist_birth_date',
'meta-value' => $today,
'compare' => '='
)
)
);
$the_query2 = new WP_Query( $args2 );
?>

Meta Query compare if meta key is in specified range

Posts have a price custom meta field associated with them. On frontend, there is an option to search a post by specifying a price range, such as all posts that's 'price' value is bigger than 1 AND smaller than 10.
Management decided that we have no option to use WooCommerce for whatever out-of-earth reason, so we have to use native Wordpress for this.
This is what I've managed to do as of now as the arguements of get_posts($args)
$args=array(
'meta_query' => array(
array(
'key' => 'price',
'value' => 1,
'compare' => '>'
)
)
);
But this only returns posts where 'price' is bigger than 1. I need to have posts which's 'price' field is bigger than 1 AND at the same time smaller than 10.
Is this possible?
Try like this:
$args=array(
'meta_query' => array(
array(
'key' => 'price',
'value' => array(1,10),
'compare' => 'BETWEEN'
)
)
);

Resources