• Resolved Dave

    (@dvaer)


    Hi there,

    After updating Filter & Grids (YMC Smart Filter) to the latest major version (v3+), my custom callback that used to work perfectly is no longer recognized by the plugin. I’ve followed the new documentation and upgrade notes, but the plugin still says the function can’t be found.

    Context

    I’m using a custom callback to enable true AND filtering for a single taxonomy on a “project” custom post type.

    This worked fine before the architecture change. My function was defined in a mu-plugin (a functionality plugin) and whitelisted via YMC_CALLBACK_FUNCTION_WHITELIST, and selected in the grid’s “Callback (theme function)” field, like this:

    After the update, the grid no longer detects or executes the callback. Instead I now see a notice that no callbacks are found:

    This is my code (in a mu-plugin):

    // Whitelist the function for YMC Smart Filter
    if ( ! defined( 'YMC_CALLBACK_FUNCTION_WHITELIST' ) ) {
    define( 'YMC_CALLBACK_FUNCTION_WHITELIST', array(
    'ww_filter_cpt_by_terms'
    ) );
    }


    /**
    * Reusable filter callback for multiple CPTs and taxonomies.
    * Supports true AND filtering with fallback to "show all" when no filters are applied.
    */

    // Custom callback function for YMC Smart Filter
    function ww_filter_cpt_by_terms( $atts ) {
    $cpt = isset($atts['cpt'][0]) ? sanitize_key($atts['cpt'][0]) : 'post';
    $taxonomy = isset($atts['tax'][0]) ? sanitize_key($atts['tax'][0]) : '';
    $terms = isset($atts['term']) && is_array($atts['term']) ? array_map('intval', $atts['term']) : [];

    if ( empty($taxonomy) ) {
    // No taxonomy to filter by – return all posts for this CPT
    return [
    'post_type' => $cpt,
    'posts_per_page' => -1,
    'post_status' => 'publish',
    ];
    }

    // Get all active (non-empty) term IDs in this taxonomy
    $active_term_ids = get_terms([
    'taxonomy' => $taxonomy,
    'fields' => 'ids',
    'hide_empty' => true,
    ]);

    /**
    * Detect if this is the plugin's default "all filters preselected" case.
    * If all active terms are in the selected list, return all posts instead.
    */

    /**
    * Important:
    * When no filters are selected on the frontend,
    * the plugin still sends *all* active terms to this callback.
    * This makes it look like filters were applied, when they weren't.
    *
    * To detect this, we check if the selected terms match all active terms.
    * If so, we return all posts (i.e., no filtering).
    *
    * ⚠️ Note: If a user selects *all filters manually*, or a post happens
    * to have *all categories*, this will still show all posts.
    * This is acceptable in our use case, but worth knowing.
    */
    if ( empty($terms) || count($terms) === count($active_term_ids) ) {
    return [
    'post_type' => $cpt,
    'posts_per_page' => -1,
    'post_status' => 'publish',
    ];
    }

    // Apply AND filter logic
    return [
    'post_type' => $cpt,
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'tax_query' => array(
    array(
    'taxonomy' => $taxonomy,
    'field' => 'id',
    'terms' => $terms,
    'operator' => 'AND',
    ),
    ),
    ];
    }

    Would you be able to point me in the right direction with this?

    Many thanks in advance for your help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author YMC

    (@wssoffice21)

    Hi!
    The new version 3+ of the plugin is completely incompatible with the old version 2+. All settings, hooks, and other functions are incompatible with the new version. You can roll back to the old version of the plugin by selecting the Legacy Mode option in the plugin settings – https://prnt.sc/IUeBvuEsxYL-
    Or, if you want to use the new version of the plugin, you should read the new documentation. Everything is described in detail there, with examples. Here’s the link: https://github.com/YMC-22/Filter-Grids
    The old version of the plugin is no longer supported, so we recommend using the new version.
    We hope we were able to help you!

    Plugin Author YMC

    (@wssoffice21)

    Thread Starter Dave

    (@dvaer)

    Thank you for pointing me in the right direction. This is now working again for version 3 of the plugin. I’m sharing my new code below for reference.

    Thanks again 🙏

    /**
    * FILTERS & GRIDS v3+ — callback-based advanced query
    */

    /**
    * 1) Whitelist callback (global).
    */
    add_filter('ymc/filter/query/wp/allowed_callbacks', function ($callbacks) {
    $callbacks[] = 'ww_filter_cpt_by_terms_v3';
    return $callbacks;
    });


    /**
    * 2) The callback: returns WP_Query args.
    *
    * @param array $ctx {
    * @type array $post_type List of post type slugs.
    * @type array $taxonomy List of taxonomy slugs.
    * @type array $terms List of term IDs to filter by.
    * @type int $page_id Current page ID.
    * }
    * @return array WP_Query args
    */
    function ww_filter_cpt_by_terms_v3( $ctx ) {
    // Normalize inputs from plugin context
    $post_types = !empty($ctx['post_type']) && is_array($ctx['post_type'])
    ? array_map('sanitize_key', $ctx['post_type'])
    : array('post');

    // We implement AND within a single taxonomy (use the first provided).
    $taxonomy = !empty($ctx['taxonomy']) && is_array($ctx['taxonomy'])
    ? sanitize_key($ctx['taxonomy'][0])
    : '';

    $terms = !empty($ctx['terms']) && is_array($ctx['terms'])
    ? array_values(array_filter(array_map('intval', $ctx['terms'])))
    : array();

    // Base query (also used for "show all" fallback)
    $base = array(
    'post_type' => $post_types,
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'ignore_sticky_posts' => true,
    );

    // No taxonomy provided? Show all for the chosen post type(s).
    if (empty($taxonomy)) {
    return $base;
    }

    // Determine “all selected” to trigger the fallback.
    $active_term_ids = get_terms(array(
    'taxonomy' => $taxonomy,
    'fields' => 'ids',
    'hide_empty' => true,
    ));
    if (is_wp_error($active_term_ids)) {
    $active_term_ids = array();
    }

    // Fallback to "show all" when no terms selected OR effectively all selected
    if (empty($terms) || ($active_term_ids && count(array_unique($terms)) >= count($active_term_ids))) {
    return $base;
    }

    // True AND filtering within the single taxonomy selected.
    $base['tax_query'] = array(
    array(
    'taxonomy' => $taxonomy,
    'field' => 'term_id', // important: use term_id
    'terms' => $terms,
    'operator' => 'AND',
    ),
    );

    return $base;
    }
    Plugin Author YMC

    (@wssoffice21)

    Good Luck!

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.