Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Derek Storkey

    (@dstorkey)

    I tried to use the example you referenced but this example deals with queries in the global loop, and I am using loops based on new WP_Query.
    I have two such loops on a single page. When I use the code below, the first loop, which should show 2 items, returns no results, but the two results are added to the global loop and are displayed at the bottom.
    The second loop works and the 9 items it should return are returned correctly.
    Is it possible to tell me what I am doing wrong, or to point me to an example of bypassing relevanssi in a ‘new WP_Query’ loop?
    Separately from that, I also tried returning false from the relevanssi_prevent_default_request filter, but the filter hook was never fired.

    
        /**
         * approve the search when there are active filters but no search term
         * @param $search_ok
         */
        public function rlv_search_trigger($search_ok){
            return $this->useRelevanssi ? $search_ok : true;
        }
    
        /**
         * return results when there are active filters but no search term
         * @param  array $hits $hits[0] is a list of matched WP_Post objects
         *                     $hits[1] is the search term if any
         * @return array       $hits
         *
         */
        public function rlv_hits_filter($hits){
            error_log("rlv_hits_filter ".($hits[0] ? " have hits" : " no hits"));
    
            if (!$this->useRelevanssi){
                $this->thequery->set('nopaging', true); // I do not know why this is needed
    
                // First attempt, from the article, first loop returns 0 but the results are instead added to the global wp_query. The second loop works.
                //$hits[0] = get_posts( $this->thequery->query_vars );
    
                // Second attempt, trying a separate, standalone WP_Query to get the posts. Same result.
                $tmp = new WP_Query($this->thequery->query_vars);
                $hits[0] = $tmp->posts;
    
                // Third attempt, trying to put the data directly into the query that triggered the filter hook, returns 0 results in both cases.
                //$this->thequery->posts = $tmp->posts;
            }
            error_log("rlv_hits_filter  rerun the query with get_posts ".($hits[0] ? " have hits" : " no hits"));
    
            $this->remove_filters();
            return $hits;
        }
    
    Thread Starter Derek Storkey

    (@dstorkey)

    Thank you very much for your response.

    I tried to follow your suggestion and coded

            $queryargs = $this->toArray();
            error_log("Actual query ".print_r($queryargs,true));
            $q = new WP_Query();
            $q->parse_query($queryargs);
            relevanssi_do_query($q);
    

    When there was an ‘s’ in the query it works. However, if there isnt, it doesnt return anything. (This particular query should return 2 rows, when relevanssi is deactivated).

    The actual query is

    
    Actual query Array
    (
        [posts_per_page] => 2
        [post_type] => Array
            (
                [0] => gsg_report
            )
    
        [orderby] => Array
            (
                [meta_value] => DESC
                [post_date] => DESC
            )
    
        [order] => ASC
        [meta_key] => sequence
    )
    

    Have I misunderstood how to code it, or is there something specific about this query that means it doesnt work? If I fall back to creating WP_Query($queryargs) that doesnt return anything either.

    regards,
    Derek

    Thread Starter Derek Storkey

    (@dstorkey)

    Hi Mikko,
    I may have misled you – I am seeing queries which do not set “s” working as expected. When I add the hook you supplied, the hook is not triggered for a query which does not have “s” set.

    My problem is that I have given the user the possibility of adding not just custom taxonomy filters but also a text search filter in the page. If Relevanssi is deactivated, this returns results, if Relevanssi is activated, this does not return results. I do it using separate instances of the WP_query class, not touching the main query for the page.

    So I suppose

    This is a typical query (argument to new WP_query())

    (
    [posts_per_page] => -1
    [post_type] => Array
    (
    [0] => gsg_casestudy
    [1] => gsg_report
    )

    [tax_query] => Array
    (
    [relation] => AND
    [0] => Array
    (
    [taxonomy] => supdem
    [field] => term_id
    [terms] => 52
    )

    )

    [s] => readiness
    [orderby] => Array
    (
    [meta_value] => DESC
    [post_date] => DESC
    )

    [order] => ASC
    [meta_key] => sequence
    )

    and this is what your hook outputs in this case:

    bool(true) object(WP_Query)#3407 (47) { [“query”]=> array(7) { [“posts_per_page”]=> int(-1) [“post_type”]=> array(2) { [0]=> string(13) “gsg_casestudy” [1]=> string(10) “gsg_report” } [“tax_query”]=> array(2) { [“relation”]=> string(3) “AND” [0]=> array(3) { [“taxonomy”]=> string(6) “supdem” [“field”]=> string(7) “term_id” [“terms”]=> int(52) } } [“s”]=> string(9) “readiness” [“orderby”]=> array(2) { [“meta_value”]=> string(4) “DESC” [“post_date”]=> string(4) “DESC” } [“order”]=> string(3) “ASC” [“meta_key”]=> string(8) “sequence” } [“query_vars”]=> &array(71) { [“posts_per_page”]=> int(-1) [“post_type”]=> array(2) { [0]=> string(13) “gsg_casestudy” [1]=> string(10) “gsg_report” } [“tax_query”]=> array(2) { [“relation”]=> string(3) “AND” [0]=> array(3) { [“taxonomy”]=> string(6) “supdem” [“field”]=> string(7) “term_id” [“terms”]=> int(52) } } [“s”]=> string(9) “readiness” [“orderby”]=> array(2) { [“meta_value”]=> string(4) “DESC” [“post_date”]=> string(4) “DESC” } [“order”]=> string(3) “ASC” [“meta_key”]=> string(8) “sequence” [“error”]=> string(0) “” [“m”]=> string(0) “” [“p”]=> int(0) [“post_parent”]=> string(0) “” [“subpost”]=> string(0) “” [“subpost_id”]=> string(0) “” [“attachment”]=> string(0) “” [“attachment_id”]=> int(0) [“name”]=> string(0) “” [“static”]=> string(0) “” [“pagename”]=> string(0) “” [“page_id”]=> int(0) [“second”]=> string(0) “” [“minute”]=> string(0) “” [“hour”]=> string(0) “” [“day”]=> int(0) [“monthnum”]=> int(0) [“year”]=> int(0) [“w”]=> int(0) [“category_name”]=> string(0) “” [“tag”]=> string(0) “” [“cat”]=> string(0) “” [“tag_id”]=> string(0) “” [“author”]=> string(0) “” [“author_name”]=> string(0) “” [“feed”]=> string(0) “” [“tb”]=> string(0) “” [“paged”]=> int(0) [“meta_value”]=> string(0) “” [“preview”]=> string(0) “” [“sentence”]=> string(0) “” [“title”]=> string(0) “” [“fields”]=> string(0) “” [“menu_order”]=> string(0) “” [“embed”]=> string(0) “” [“category__in”]=> array(0) { } [“category__not_in”]=> array(0) { } [“category__and”]=> array(0) { } [“post__in”]=> array(0) { } [“post__not_in”]=> array(0) { } [“post_name__in”]=> array(0) { } [“tag__in”]=> array(0) { } [“tag__not_in”]=> array(0) { } [“tag__and”]=> array(0) { } [“tag_slug__in”]=> array(0) { } [“tag_slug__and”]=> array(0) { } [“post_parent__in”]=> array(0) { } [“post_parent__not_in”]=> array(0) { } [“author__in”]=> array(0) { } [“author__not_in”]=> array(0) { } [“ignore_sticky_posts”]=> bool(false) [“suppress_filters”]=> bool(false) [“cache_results”]=> bool(true) [“update_post_term_cache”]=> bool(true) [“lazy_load_term_meta”]=> bool(true) [“update_post_meta_cache”]=> bool(true) [“nopaging”]=> bool(true) [“comments_per_page”]=> string(2) “50” [“no_found_rows”]=> bool(false) [“search_terms_count”]=> int(1) [“search_terms”]=> array(1) { [0]=> string(9) “readiness” } [“search_orderby_title”]=> array(1) { [0]=> string(43) “wp_posts.post_title LIKE ‘%readiness%'” } [“taxonomy”]=> string(6) “supdem” [“term_id”]=> int(52) } [“tax_query”]=> object(WP_Tax_Query)#3405 (6) { [“queries”]=> array(2) { [“relation”]=> string(3) “AND” [0]=> array(5) { [“taxonomy”]=> string(6) “supdem” [“terms”]=> array(1) { [0]=> int(52) } [“field”]=> string(7) “term_id” [“operator”]=> string(2) “IN” [“include_children”]=> bool(true) } } [“relation”]=> string(3) “AND” [“table_aliases”:protected]=> array(1) { [0]=> string(26) “wp_term_relationships” } [“queried_terms”]=> array(1) { [“supdem”]=> array(2) { [“terms”]=> array(1) { [0]=> int(52) } [“field”]=> string(7) “term_id” } } [“primary_table”]=> string(13) “wp_posts” [“primary_id_column”]=> string(2) “ID” } [“meta_query”]=> object(WP_Meta_Query)#3406 (9) { [“queries”]=> array(2) { [0]=> array(1) { [“key”]=> string(8) “sequence” } [“relation”]=> string(2) “OR” } [“relation”]=> string(3) “AND” [“meta_table”]=> string(16) “wp_postmeta” [“meta_id_column”]=> string(7) “post_id” [“primary_table”]=> string(13) “wp_posts” [“primary_id_column”]=> string(2) “ID” [“table_aliases”:protected]=> array(1) { [0]=> string(16) “wp_postmeta” } [“clauses”:protected]=> array(1) { [“wp_postmeta”]=> array(4) { [“key”]=> string(8) “sequence” [“compare”]=> string(1) “=” [“alias”]=> string(16) “wp_postmeta” [“cast”]=> string(4) “CHAR” } } [“has_or_relation”:protected]=> bool(false) } [“date_query”]=> bool(false) [“request”]=> string(783) “SELECT wp_posts.* FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (52) ) AND (((wp_posts.post_title LIKE ‘%readiness%’) OR (wp_posts.post_excerpt LIKE ‘%readiness%’) OR (wp_posts.post_content LIKE ‘%readiness%’))) AND ( wp_postmeta.meta_key = ‘sequence’ ) AND wp_posts.post_type IN (‘gsg_casestudy’, ‘gsg_report’) AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_author = 1 AND wp_posts.post_status = ‘private’) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value DESC, wp_posts.post_date DESC ” [“post_count”]=> int(0) [“current_post”]=> int(-1) [“in_the_loop”]=> bool(false) [“comment_count”]=> int(0) [“current_comment”]=> int(-1) [“found_posts”]=> int(0) [“max_num_pages”]=> int(0) [“max_num_comment_pages”]=> int(0) [“is_single”]=> bool(false) [“is_preview”]=> bool(false) [“is_page”]=> bool(false) [“is_archive”]=> bool(true) [“is_date”]=> bool(false) [“is_year”]=> bool(false) [“is_month”]=> bool(false) [“is_day”]=> bool(false) [“is_time”]=> bool(false) [“is_author”]=> bool(false) [“is_category”]=> bool(false) [“is_tag”]=> bool(false) [“is_tax”]=> bool(true) [“is_search”]=> bool(true) [“is_feed”]=> bool(false) [“is_comment_feed”]=> bool(false) [“is_trackback”]=> bool(false) [“is_home”]=> bool(false) [“is_404”]=> bool(false) [“is_embed”]=> bool(false) [“is_paged”]=> bool(false) [“is_admin”]=> bool(false) [“is_attachment”]=> bool(false) [“is_singular”]=> bool(false) [“is_robots”]=> bool(false) [“is_posts_page”]=> bool(false) [“is_post_type_archive”]=> bool(false) [“query_vars_hash”:”WP_Query”:private]=> string(32) “4c340a6aa25b5669994da50cce469751” [“query_vars_changed”:”WP_Query”:private]=> bool(false) [“thumbnails_cached”]=> bool(false) [“stopwords”:”WP_Query”:private]=> array(29) { [0]=> string(5) “about” [1]=> string(2) “an” [2]=> string(3) “are” [3]=> string(2) “as” [4]=> string(2) “at” [5]=> string(2) “be” [6]=> string(2) “by” [7]=> string(3) “com” [8]=> string(3) “for” [9]=> string(4) “from” [10]=> string(3) “how” [11]=> string(2) “in” [12]=> string(2) “is” [13]=> string(2) “it” [14]=> string(2) “of” [15]=> string(2) “on” [16]=> string(2) “or” [17]=> string(4) “that” [18]=> string(3) “the” [19]=> string(4) “this” [20]=> string(2) “to” [21]=> string(3) “was” [22]=> string(4) “what” [23]=> string(4) “when” [24]=> string(5) “where” [25]=> string(3) “who” [26]=> string(4) “will” [27]=> string(4) “with” [28]=> string(3) “www” } [“compat_fields”:”WP_Query”:private]=> array(2) { [0]=> string(15) “query_vars_hash” [1]=> string(18) “query_vars_changed” } [“compat_methods”:”WP_Query”:private]=> array(2) { [0]=> string(16) “init_query_flags” [1]=> string(15) “parse_tax_query” } }

    So I suppose my question is whether it is possible to still use Relevanssi, not on a search results page, but a normal page, using a separate WP_query instance.

    thanks

    Can I add a small comment? I have this problem as well – it makes all non-public post types uneditable.

    The fix suggested above isn’t quite right as it ignores the possibility that a filter hook has changed the criteria.

    Also, perhaps there are two options: either don’t show the post type switcher, as Mr van Kooten suggests, or always add the current post’s type to the list, which would still allow you to switch the type of a non-public post to some other public type.

    My suggestion after line 75:

    <?php if (!in_array($cpt_object, $post_types)) $post_types[get_post_type()] = $cpt_object; ?>

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