Exclude media from front search

  • dnikola
    Participant
    2 years, 8 months ago #19149

    Hello Partice,

    here is my current settings on data 2.2 page https://prntscr.com/ro2rxg
    How I can change a option to “Index all status. To replace search in admin too. Only published is shown in front search.” but I don’t want to show them on front.
    I want to index them and on wp admin to replace query’s on media page.

    wpsolr
    Keymaster
    2 years, 8 months ago #19157

    There is a protection in WPSOLR that prevents to show on front-end any post type defined as ‘exclude_from_search’ => true. For instance WooCommerce orders will never be shown on front-end, even if selected in 2.2 to be indexed for search admin.

    If medias are not excluded from search, they will show up in WPSOLR front-end search results.

    dnikola
    Participant
    2 years, 8 months ago #19159

    You mean that I use something like this

    add_action( ‘init’, ‘exclude_images_from_search_results’ );
    function exclude_images_from_search_results() {
    global $wp_post_types;

    $wp_post_types[‘attachment’]->exclude_from_search = true;
    }

    wpsolr
    Keymaster
    2 years, 8 months ago #19161

    It must prevent attachments to be returned by:
    get_post_types( [‘exclude_from_search’ => false] ) )

    • This reply was modified 2 years, 8 months ago by wpsolr.
    dnikola
    Participant
    2 years, 8 months ago #19162

    Ok I will test.

    Thanks

    dnikola
    Participant
    2 years, 7 months ago #19423

    I made few test and come back to report but now i see that you edited your first answare.
    Ok with wp solr I am still getting images showing up
    I am using this code

    // Exclude images from search results – WordPress
    function onlyposts_filter_wp_search($query) {
    //Filter Search only in the front-end and not in the Admin.
    if (!$query->is_admin && $query->is_search) {
    //Search in Posts only. Excludes Pages, Attachments and CPTs.
    $query->set(‘post_type’, ‘post’);
    }
    return $query;
    }
    add_filter(‘pre_get_posts’,’onlyposts_filter_wp_search’);

    but without wpsolr only posts show up.
    What did you want to tell with this get_post_types( [‘exclude_from_search’ => false] ) ) ?

    dnikola
    Participant
    2 years, 7 months ago #19424

    Ok,

    figured out

    this helped.

    add_action( ‘init’, ‘exclude_from_search’ );

    function exclude_from_search() {
    global $wp_post_types;
    /* Exclude ‘page’ post type. */
    $wp_post_types[‘page’]->exclude_from_search = true;
    /* Exclude ‘attachment’ post type. */
    $wp_post_types[‘attachment’]->exclude_from_search = true;
    }

    but you could have this as option in plugin settings ..

    wpsolr
    Keymaster
    2 years, 7 months ago #19426

    I moved your topic to “New feature requests”.

    The idea is to control which post types is visible on front-end search and on back-end search.

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

You must be logged in to reply to this topic.