Exclude posts based on custom acf field value

  • 1 year, 7 months ago #31061

    Hi, How can we exclude a custom field set to No, in product data. I tried this code and also use pre_get_posts, still nothing worked. Can you suggest solution for this.

    // functions.php //
    add_filter(WPSOLR_Events::WPSOLR_ACTION_POSTS_RESULTS,’ search_filter’, 99, 5);
    function search_filter($query) {
    // Frontend search only
    if ( ! is_admin() ) {
    //custom field check
    $meta_query = array(
    array(
    ‘key’ => ‘_show’,
    ‘value’ => ‘YES’,
    ‘compare’ => ‘==’,
    )
    );
    $query->set(‘meta_query’, $meta_query);
    }
    return $query;
    }

    wpsolr
    Keymaster
    1 year, 7 months ago #31062

    This hook is not meant for filtering results, but to update results with custom values.

    You can filter with no hook: configure your ACF field as a checkbox filter, then pre-select the ‘YES’ value.
    Example is shown in below capture, where filter “type” is preselected with value “attachment”.

    Image wpsolr-fecet-preselect.png of

    1 year, 7 months ago #31069

    Adding this has added filter in frontend with acf field, with Yes prechecked, we dont need any filter in front end, we wanted to exclude indexing of product based on ACF field value if its set to NO.

    wpsolr
    Keymaster
    1 year, 7 months ago #31072

    To exclude some posts from the index, you could modify the SQL query as described on https://www.wpsolr.com/forums/topic/hide-products-with-empty-or-zero-price/

    1 year, 6 months ago #31286

    Image image.png of
    Can’t we use this to hide products based on Value ‘NO’, regardless of if the same field exists with Yes, empty or even doesn’t exists for other posts ? Currently we have to use preselect Yes or No, for this to work, I want to hide those with No…

    wpsolr
    Keymaster
    1 year, 6 months ago #31288

    This “Hide” option hides the facet value. It does not filter out results based on the facet value.

    1 year, 6 months ago #31292

    Can we create a custom field, and push data to that field, while indexing and use that to hide or show results. I want to set a create a field, if that meta_key isn’t available for a post id, and set the value to Yes.
    We have ACF meta_key , which is either es, No or its not available for 80% of posts, when meta_key is not present, I want to create field and set to Yes.
    Some sort of this code ?
    $doc2->setField(‘id’, 124);
    $doc2->setField(‘name’, ‘testdoc-2’);

    wpsolr
    Keymaster
    1 year, 6 months ago #31293

    I don’t think so.

    But if you want to prevent some posts to be indexed, edit the post, select the “Do not search” in the WPSOLR’s meta box, and save.

    1 year, 6 months ago #31294

    we have around 2L posts, so this wont work for us.

    wpsolr
    Keymaster
    1 year, 6 months ago #31295

    To exclude some posts from the index, you could modify the SQL query as described on https://www.wpsolr.com/forums/topic/hide-products-with-empty-or-zero-price/

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

You must be logged in to reply to this topic.