Home › Forums › Pro Support › Modify Query – Filter CPT by ACF date field
- This topic has 11 replies, 2 voices, and was last updated 2 years, 1 month ago by
elvin.
-
AuthorPosts
-
August 6, 2021 at 7:57 am #32792
Xavier
ParticipantHi, although I have read different topics I can’t find a solution for what I need.
I have a custom post type for events with a custom field for the date.
I would like to query the posts of this custom post type using the data of this custom post field.
The idea is to show only the upcoming events (the date value should be settled in the future) and order from the closest in time to the latest.
The custom field I have created for the date is: fecha
And the format of the date: d/m/Y (it it is necessary I can change this).Is there any example of code I should use to achieve this?
I have looked at the documentation, but I couldn’t find the exact way.
Many thanks.
August 8, 2021 at 7:04 pm #32822elvin
ModeratorHi there,
I believe what you’re trying to do will mainly revolve around Tom’s suggestion here. (with a few modification)
https://wpshowposts.com/support/topic/can-i-exclude-with-a-custom-meta-field-from-a-list/#post-13344
Or my suggestion here:
https://wpshowposts.com/support/topic/feature-request-detect-and-cooperate-with-plugin-post-views-counter/#post-28475Example:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 123 === $settings['list_id'] ) { $args['post_type'] = array('post','page','custom_post_type_slug_1'); $args['meta_query'] = array( array( 'key' => 'your_custom_field', 'orderby' => 'meta_value_num', 'order' => 'DESC', ) ); } return $args; } );
replace the value of $args[‘post_type’] and $args[‘meta_query’].
August 9, 2021 at 9:23 am #32834Xavier
ParticipantHi Elvin, thanks for your fast response.
Unfortunately, when I try to use the code as a code snippet I get this error:
syntax error, unexpected ‘=>’ (T_DOUBLE_ARROW)
August 9, 2021 at 9:38 am #32836Xavier
ParticipantThe error seemps to be in the fourth line:
$args[‘meta_query’] => array(
August 9, 2021 at 7:34 pm #32843elvin
ModeratorCan you try changing
$args['meta_query'] =>
to$args['meta_query'] =
?Let us know how it goes. 😀
August 10, 2021 at 12:27 am #32844Xavier
ParticipantHi Elvin, with this change the code snippet is “fine” and the error solved, but this code breaks the design of the website completely.
The website doesn’t show the site content of any page, just the header.
August 10, 2021 at 6:08 pm #32869elvin
ModeratorThat’s strange.
This shouldn’t break the site as the code only meddles with the query args of WPSP. Can you share how you’ve added the code? (and the exact code added)
August 11, 2021 at 1:22 am #32872Xavier
ParticipantHi Elvin, I paste this code to the code snippets plugin:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 393 === $settings['list_id'] ) { $args['post_type'] = array('post','page','evento'); $args['meta_query'] = array( array( 'key' => 'fecha', 'orderby' => 'meta_value_num', 'order' => 'DESC', ) ); } return $args; } );
August 11, 2021 at 10:15 pm #32887elvin
ModeratorStrange.
The code looks good.
But let’s try this:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 393 === $settings['list_id'] ) { $args['post_type'] = array('post','page','evento'); $args['meta_query'] = array( array( 'key' => 'fecha', 'order' => 'DESC', ) ); } return $args; }, 20, 2 );
Let us know if anything changes.
Also, can you link us to the page that are breaking? To check and get and idea.
August 12, 2021 at 12:28 am #32888Xavier
ParticipantHi Elvin,
this code seems correct (the design doesn’t break anymore), but it doesn’t do anything. The elements still order by the date of posting instead of the date of the custom field.
If you want I can create an acces to the website, just tell me where I can sell the user and pass.
Thanks again.
August 12, 2021 at 2:39 am #32890Xavier
ParticipantBy the way, I have detected another thing strange.
I have had installed the version 1.2 beta of the plugin. I thougt, may be the problem is the version of the plugin. When I have downgraded to the current version, with the new code, the design broke again.
August 12, 2021 at 9:00 pm #32900elvin
ModeratorYou can send the details through our contact page here – https://wpshowposts.com/contact/
Can you share what value is being fetched from the
fecha
field? Is it a string? Or a number?We’ll have to add in
'orderby'
but it’s value will depend on what fecha field returns to fix the sorting order. 😀 -
AuthorPosts
- You must be logged in to reply to this topic.