• Hetllo,

    I am using Relevanssi plugin for search functionality. I need to add empty search term functionality. For that I have added below code.

    function rlv_hits_filter($hits) {
    global $wp_query;
    if (
    !$hits[1] &&
    isset( $wp_query->query_vars[‘post_type’]) ||
    isset( $wp_query->query_vars[‘credentials’]) ||
    isset( $wp_query->query_vars[‘zipcode’])
    ) {
    $wp_query->set(‘nopaging’, true);
    $wp_query->query_vars[‘posts_per_page’] = -1;
    $wp_query->set(‘post_type’, $wp_query->query_vars[‘post_type’]);
    $hits[0] = get_posts( $wp_query->query_vars );
    }
    return $hits;
    }
    add_filter(‘relevanssi_hits_filter’, ‘rlv_hits_filter’,1);

    But when I have checked this code not returns all the posts. I have 380 published posts but it returns 255.

    Can you please advise me how I solve this issue.

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Try debugging the function. What exactly does get_posts() return? Is there anything common to the posts that are missing?

    Have you tried what happens without this function? Relevanssi does have the empty search term functionality built in, you shouldn’t need this function at all.

    Thread Starter sguha007

    (@sguha007)

    Hello,

    Have you tried what happens without this function?

    — Yes. If I remove this function and do search without search term nothing is coming.

    Relevanssi does have the empty search term functionality built in

    — Can you please share the “the empty search term functionality” built in function

    Plugin Author Mikko Saari

    (@msaari)

    There’s no function. If you just do a search without any search term, Relevanssi should return all results that match the current query parameters. Relevanssi should run a query like this:

    SELECT DISTINCT(relevanssi.doc), relevanssi.*, relevanssi.title * 5 + relevanssi.content * 5 + relevanssi.comment * 0.75 + relevanssi.tag * 0.5 + relevanssi.link * 0.75 + relevanssi.author + relevanssi.category * 0.5 + relevanssi.excerpt + relevanssi.taxonomy + relevanssi.customfield + relevanssi.mysqlcolumn AS tf
    FROM wp_relevanssi AS relevanssi
    WHERE relevanssi.term = relevanssi.term
    ORDER BY tf DESC

    This finds all posts. If you check your site with Query Monitor, do you get a similar result if you run a query without a search term?

    Thread Starter sguha007

    (@sguha007)

    Hello,

    get_posts() return first 15 posts because pagination are there. But not coming all 380 posts.

    Plugin Author Mikko Saari

    (@msaari)

    Since you’re using the global $wp_query, I’d check to see what it contains. Perhaps there are other parameters in it you’re not noticing?

    (Also you can remove this: $wp_query->set(‘post_type’, $wp_query->query_vars[‘post_type’]); – that’s just setting the parameter to the value it currently has, ie. that line does nothing.)

    Thread Starter sguha007

    (@sguha007)

    Hello,

    I have remove this

    $wp_query->set(‘post_type’, $wp_query->query_vars[‘post_type’]);

    Here is the $wp_query result

    WP_Query Object
    (
    [query] => Array
    (
    [s] =>
    [post_type] => vendors
    )

    [query_vars] => Array
    (
    [s] =>
    [post_type] => vendors
    [error] =>
    [m] =>
    [p] => 0
    [post_parent] =>
    [subpost] =>
    [subpost_id] =>
    [attachment] =>
    [attachment_id] => 0
    [name] =>
    [pagename] =>
    [page_id] => 0
    [second] =>
    [minute] =>
    [hour] =>
    [day] => 0
    [monthnum] => 0
    [year] => 0
    [w] => 0
    [category_name] =>
    [tag] =>
    [cat] =>
    [tag_id] =>
    [author] =>
    [author_name] =>
    [feed] =>
    [tb] =>
    [paged] => 0
    [meta_key] =>
    [meta_value] =>
    [preview] =>
    [sentence] =>
    [title] =>
    [fields] =>
    [menu_order] =>
    [embed] =>
    [category__in] => Array
    (
    )

    [category__not_in] => Array
    (
    )

    [category__and] => Array
    (
    )

    [post__in] => Array
    (
    )

    [post__not_in] => Array
    (
    )

    [post_name__in] => Array
    (
    )

    [tag__in] => Array
    (
    )

    [tag__not_in] => Array
    (
    )

    [tag__and] => Array
    (
    )

    [tag_slug__in] => Array
    (
    )

    [tag_slug__and] => Array
    (
    )

    [post_parent__in] => Array
    (
    )

    [post_parent__not_in] => Array
    (
    )

    [author__in] => Array
    (
    )

    [author__not_in] => Array
    (
    )

    [posts_per_page] => 15
    [ignore_sticky_posts] =>
    [suppress_filters] =>
    [cache_results] => 1
    [update_post_term_cache] => 1
    [lazy_load_term_meta] => 1
    [update_post_meta_cache] => 1
    [nopaging] =>
    [comments_per_page] => 50
    [no_found_rows] =>
    [order] => DESC
    [operator] => OR
    )

    [tax_query] => Array
    (
    )

    [meta_query] => WP_Meta_Query Object
    (
    [queries] => Array
    (
    )

    [relation] =>
    [meta_table] =>
    [meta_id_column] =>
    [primary_table] =>
    [primary_id_column] =>
    [table_aliases:protected] => Array
    (
    )

    [clauses:protected] => Array
    (
    )

    [has_or_relation:protected] =>
    )

    [date_query] =>
    [request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = ‘vendors’ AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘acf-disabled’) ORDER BY wp_posts.post_date DESC LIMIT 0, 15
    [posts] => Array
    (
    [0] => WP_Post Object
    (
    [ID] => 20650
    [post_author] => 113
    [post_date] => 2020-04-07 16:12:39
    [post_date_gmt] => 2020-04-07 16:12:39
    [post_content] => Crown + Conquer is a fully independent agency driven by the belief that fostering personal and honest relationships and developing those relationships in an effort to help brands reach consumers through authentic partnerships is the most powerful, yet valuable weapon.
    [post_title] => Crown + Conquer
    [post_excerpt] =>
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] =>
    [post_name] => crown-conquer
    [to_ping] =>
    [pinged] =>
    [post_modified] => 2020-04-07 16:14:56
    [post_modified_gmt] => 2020-04-07 16:14:56
    [post_content_filtered] =>
    [post_parent] => 0
    [guid] => https://onesandbox.com/?post_type=vendors&p=20650
    [menu_order] => 0
    [post_type] => vendors
    [post_mime_type] =>
    [comment_count] => 0
    [filter] => raw
    [lat] =>
    [lon] =>
    [name] => Crown + Conquer
    [numLikes] => 0
    )

    Then show first 15 posts

    Plugin Author Mikko Saari

    (@msaari)

    And this is the get_posts() request in the filter you now have? What happens if you now add this:

    $wp_query->set( 'posts_per_page', -1 );

    to the query?

    Thread Starter sguha007

    (@sguha007)

    Hetllo,

    I didn’t understand. I have already added this

    $wp_query->set( ‘posts_per_page’, -1 );

    Here:

    function rlv_hits_filter($hits) {
    global $wp_query;
    if (
    !$hits[1] &&
    isset( $wp_query->query_vars[‘post_type’]) ||
    isset( $wp_query->query_vars[‘credentials’]) ||
    isset( $wp_query->query_vars[‘zipcode’])
    ) {
    $wp_query->set(‘nopaging’, true);
    $wp_query->query_vars[‘posts_per_page’] = -1;
    }
    return $hits;
    }
    add_filter(‘relevanssi_hits_filter’, ‘rlv_hits_filter’,1);

    Plugin Author Mikko Saari

    (@msaari)

    I don’t understand either. Was the get_posts() parameters and output you showed me from that function, or somewhere else?

    Let’s simplify things a bit. What do you get if you replace the filter with this:

    function rlv_hits_filter($hits) {
    $args = array(
        'post_type' => 'vendors',
        'posts_per_page' => -1,
    );
    $hits[1] = get_posts( $args );
    return $hits;
    }
    add_filter('relevanssi_hits_filter', 'rlv_hits_filter',1);

    When you use this, now every search should return you all posts from the vendors post type. Does that happen? (I know that’s not what you want eventually, but for the sake of simplicity, we need to have this work first.)

    • This reply was modified 4 years, 7 months ago by Mikko Saari.
    Thread Starter sguha007

    (@sguha007)

    Hello,

    Yes I have already done this

    $args = array(‘post_type’ => ‘vendors’,’posts_per_page’ => -1);
    $hits[0] = get_posts($args);

    when I use this, it return all 380 posts but filtering not working

    Plugin Author Mikko Saari

    (@msaari)

    Ok, there you have something that works, and now you can just start adding the filtering parameters one by one.

    Also, if you have filtering parameters, why are you expecting to get all posts as results? Could it be that those 225 posts returned by the function are actually the ones that match the filters?

    Thread Starter sguha007

    (@sguha007)

    Yes there is filtering parameter

    if (
    !$hits[1] &&
    isset( $wp_query->query_vars[‘post_type’]) ||
    isset( $wp_query->query_vars[‘credentials’]) ||
    isset( $wp_query->query_vars[‘zipcode’])
    )

    Plugin Author Mikko Saari

    (@msaari)

    I have nothing more to offer here. Start from what works, then add filters and constrictions one by one and make sure every change works, and you’ll end up with something that does what you expect it to do.

    Thread Starter sguha007

    (@sguha007)

    Then why this $wp_query->query_vars not return 380 posts

    Plugin Author Mikko Saari

    (@msaari)

    Because you have those filters there (credentials and zipcode). Those filter some of the posts out. If you want all posts, don’t use $wp_query which includes filters, but instead build the arguments array from scratch without those filters.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Empty Search Term’ is closed to new replies.