Support

Account

Home Forums Front-end Issues Creating a WP archive with custom field filter error

Solved

Creating a WP archive with custom field filter error

    • nobodii

    • September 28, 2019 at 11:40 pm

    Hello,
    I follow this documentation:
    https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

    Here is the code:
    ####################
    // array of filters (field key => field name)
    $GLOBALS[‘my_query_filters’] = array(
    ‘field_1’ => ‘couloc’,
    ‘field_2’ => ‘coucat’
    );

    // action
    add_action(‘pre_get_posts’, ‘my_pre_get_posts’, 10, 1);

    function my_pre_get_posts( $query ) {

    // bail early if is in admin
    if( is_admin() ) return;

    // bail early if not main query
    // – allows custom code / plugins to continue working
    if( !$query->is_main_query() ) return;

    // get meta query
    $meta_query = $query->get(‘meta_query’);

    // loop over filters
    foreach( $GLOBALS[‘my_query_filters’] as $key => $name ) {

    // continue if not found in url
    if( empty($_GET[ $name ]) ) {

    continue;

    }

    // get the value for this filter
    // eg: http://www.website.com/events?city=melbourne,sydney
    $value = explode(‘,’, $_GET[ $name ]);

    // append meta query
    $meta_query[] = array(
    ‘key’ => $name,
    ‘value’ => $value,
    ‘compare’ => ‘IN’,
    );

    }

    // update meta query
    $query->set(‘meta_query’, $meta_query);

    }
    ####################

    But when I try I have an error… (see image)
    Can you tell me why?

    • nobodii

    • September 28, 2019 at 11:42 pm

    Here are screenshot..

  • You have a fatal error in your code which I cannot see by looking at it.

    You need to disable WP WSOD protection https://wordpress.stackexchange.com/questions/337391/how-to-disable-the-fatal-error-wsod-protection

    And you need to enable debugging https://codex.wordpress.org/WP_DEBUG

    so that you can see what the real error is.

    • nobodii

    • September 29, 2019 at 1:38 am
    This reply has been marked as private.
    • nobodii

    • September 29, 2019 at 3:03 am
    This reply has been marked as private.
    • nobodii

    • September 29, 2019 at 10:45 am
    This reply has been marked as private.
    • nobodii

    • September 29, 2019 at 11:06 am

    ok I found a solution..
    I had to had this..
    $meta_query = [];

    before
    // append meta query
    $meta_query[] = array(
    ‘key’ => $name,
    ‘value’ => $value,
    ‘compare’ => ‘IN’,
    );

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.