We're merging with GenerateBlocks! Learn more here.

[Support request] Modify Query – Filter CPT by ACF date field

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Modify Query – Filter CPT by ACF date field

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #32792
    Xavier
    Participant

    Hi, 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.

    #32822
    elvin
    Moderator

    Hi 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-28475

    Example:

    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’].

    #32834
    Xavier
    Participant

    Hi 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)

    #32836
    Xavier
    Participant

    The error seemps to be in the fourth line:

    $args[‘meta_query’] => array(

    #32843
    elvin
    Moderator

    Can you try changing $args['meta_query'] => to $args['meta_query'] =?

    Let us know how it goes. 😀

    #32844
    Xavier
    Participant

    Hi 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.

    #32869
    elvin
    Moderator

    That’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)

    #32872
    Xavier
    Participant

    Hi 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;
    } );
    #32887
    elvin
    Moderator

    Strange.

    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.

    #32888
    Xavier
    Participant

    Hi 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.

    #32890
    Xavier
    Participant

    By 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.

    #32900
    elvin
    Moderator

    You 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. 😀

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.