Wordpress get_next_post based on custom date field - wordpress

I have a Wordpress news site.
The news stories are added as posts.
To have more control I have a custom date field on the posts.
The posts are displayed in date order using the date in the custom field.
So the order the posts are displayed on the site can sometimes be different from the order of
the posts in the WP backend which are in the date they were added order.
On the single.php page I have a next and prev post links.
The next and prev post links use the order of the posts in WP which can be different from that on the front end.
Can I use the WP next,prev function with a custom date field instead of the actual WP order of posts. So the next and prev links will link to the next and prev links shown in the front end of the site.

I am not sure there is a solution using get_next_post(), but I guess you could query a single post with a higher/lower date using your custom meta.
Something like this should work, but I did not test it.
$args = array(
'meta_query'=> array(
array(
'key' => 'date',
'compare' => '>=',
'value' => $currentPostDate,
'type' => 'DATE',
)
),
'posts_per_page' => 1
);
query_posts( $args );
Keep in mind this note from the documentation:
type (string) - Custom field type. Possible values are 'NUMERIC',
'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME',
'UNSIGNED'.
Default value is 'CHAR'. The 'type' DATE works with the
'compare' value BETWEEN only if the date is stored at the format
YYYY-MM-DD and tested with this format.

Related

ACF - Gallery Field -> reverse lookup to find all posts where that image is in the Gallery Field

I am trying to do a reverse query to show me all of the posts that an image may be attached to using the ID of that image.
The kicker is, the connection would be done using the ACF Gallery Field
https://www.advancedcustomfields.com/resources/gallery/
I have the ID's of the images noticed there was a meta data field for ["uploaded_to"] that I thought originally would work, but this is just a single ID, not an array, and is also only the post that the image was uploaded to and won't show if the image was uploaded directly to the media library.
The only thing I can think of which would be very resource heavy and slow would be to loop through all posts and each field of those posts for the Gallery Field and then match on the ID of the image, and when there is match, output the ID of the post to an array. This sounds just messy and bloated.
Is there an easier way to do this type of query?
It is possible to craft a WP query where you search based on meta fields.
$wp_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => -1, //query every post
'meta_query' => array(
array(
'key' => 'uploaded_to',
'value' => $value_your_looking_for,
'compare' => 'LIKE'
)
)
));
I don't know what for values the field uploaded_to returns, but I'm guessing that it is an array with images id's

Using wordpress and advanced custom fields, how can I use a subfield for my meta query?

I'm using Wordpress with the Advanced Custom Fields Pro plugin and I'm trying to get all the posts that have a date field that is after today's date. I looked at this example https://www.advancedcustomfields.com/resources/date-picker/ and that works fine except that my field is a subfield of a repeater field. And I have some trouble making a meta query for that. I tried using _%_ for the subfield but then I read here https://support.advancedcustomfields.com/forums/topic/meta_query-for-a-group-field-sub-field/ that I can just use an underscore for the subfield. But posts still aren't coming up with any results. I tried checking the date from just a single post and it appears to be working correctly. I also read here advancedcustomfields.com/resources/date-picker that the date is always stored as Ymd in the database. I tried doing it with another field that isn't a subfield and that worked, so the problem lies with the subfield I think 'key' => 'date_startdate',
I am using the date picker as a subfield.
function do_get_upcoming($atts = [])
{
$defaultLimit = 8;
$limit = $atts["limit"] != null ? $atts["limit"] : $defaultLimit;
$today = date('Ymd');
$posts = get_posts(array(
'numberposts' => $limit,
'post_type' => 'events',
'meta_query' => array(
array(
'key' => 'date_startdate',
'value' => $today,
‘type’ => ‘DATE’
'compare' => '>',
)
),
));
echo count($posts);
}
add_shortcode('get_upcoming','do_get_upcoming');
screenshot subfield
I found the solution. Subfields can be accessed by adding _0_ so you'll get 'key' => 'date_0_startdate'

How to add a new sortable date meta to a custom post type

Looking for a point in the right direction, i've been working on an events plugin for a site, quite a simple plugin, adds a custom post type called events, adds some custom taxonomies, used ACF to add the fields to the CPT.
But I need to add 'start date and time', and 'end date and time' fields to it in a way that I'll be able to sort by start date/time in the admin side, and be able to sort by them when I query the posts as well.
I thought I might be able to do this with the ACF date/time field, but it's just not playing ball. Any ideas?
If i'm understanding correctly, you want to sort your results by a meta value right? that meta value being the date/time field. In your loop, you may want to try out something along the lines of:
$your_custom_query = new WP_Query( array(
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'date_time_meta_key_here',
'compare' => 'EXISTS',
)
),
'orderby' => 'date_clause'
));
See the original wordpress documentation here for more info: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

Wordpress filters

I used wordpress to make a website with some custom types. The problem is that i need to implement a filter for these types and I have some questions about the plugins that i'm using for this project:
For to make custom types and views I'm using Types and Views plugins for wordpress.
I made a new custom type with some fields (title, price, some selectors, ...) and I need to filter all the information ordering the results following 2 criterias. I don't know if is it possible to do so using the Views/Types plugin.
I also need to filter the fields of the filter. For example, if i have 2 fields one called Countries and other called Cities, if i select Spain in countries I should see only spanish cities in the second field.
Any idea if i can do that using Types and Views plugins? is there any other plugin?
Thanks in advance!
Any good plugin will save the custom fields as post-meta. You need to find out what the key is for the field you are trying to use as a filter. Once you have that you can use a simple meta query to get the posts you need:
$args = array(
'post_type' => 'post', // The post type
'posts_per_page' => -1, // Get all posts
'orderby' => 'rand', // Random order, also can use 'post_date' or 'post_title'
'order' => 'ASC', // Direction of sort
'meta_key' => 'my_meta_key', // Whatever your key is
'meta_query' => array(
array(
'key' => 'my_meta_key', // Whatever your key is
'value' => 'foo' // What it should be
'compare' => 'IN', // How to compare the value to the stored value
)
)
);
$query = new WP_Query($args); // The query
Here is more on the Wordpress codex.

Sorting problem with 'query_posts' function in wordpress. Sort by custom field not working

I am working with wordpress where i have a event listing system. There is a custom field in my post called starting_time which is unix timestamp. Now i want to short all posts by starting_time by this query_post command:
query_posts(array(
'post_type' => 'event',
'meta_key' => 'end_time',
'meta_compare' =>'>=',
'meta_value'=>time(),
'order_by' => 'start_date',
'order' => 'ASC'
));
But it not working. Here is the site http://citystir.com/events/. I am echoing the start_time in unix before the time remaining so you can see the value. And also my pagination is not working. May be I did something wrong in the query_post command.
You need to make sure that you have the correct meta_key name used in the query. You say that the custom field is called starting_time but you set order_by to start_date in your code.
The order seems ok at ASC as this will show next up event.
The problem was with the order_by command :) it is not order_by but orderby.

Resources