[Resolved] Block Element Not Showing up on Search Results Page

Home Forums Support [Resolved] Block Element Not Showing up on Search Results Page

Home Forums Support Block Element Not Showing up on Search Results Page

Viewing 7 posts - 16 through 22 (of 22 total)
  • Author
    Posts
  • #1629585
    Tom
    Lead Developer
    Lead Developer

    The Elements query happens during the wp action so we have access to certain conditions.

    get_posts() is a wrapper for WP_Query, so it’s possible that the query needs to be reset either before or after the Elements query.

    #1629632
    Samuel

    In this case at least the reset needs to happen before the elements query. Which sounds like something that GP should handle? In the meantime is there a recommended Hook I could use to insert the reset before the elements query is run?

    #1630622
    Tom
    Lead Developer
    Lead Developer

    Can you try adding the query reset before the query to see if it does anything? If it does, I’ll dig in and make sure it’s the right action to take for our next update ๐Ÿ™‚

    #1630801
    Samuel

    I’m totally confused now. I modified elements.php like this:

    	//Reset the main query
    	wp_reset_query();
    
    	$posts = get_posts( $args );
    
    	foreach ( $posts as $post_id ) {
    		$post_id = apply_filters( 'generate_element_post_id', $post_id );
    		$type = get_post_meta( $post_id, '_generate_element_type', true );
    

    However “is_main_query()” still responds true because the global var $wp_the_query is still query to the main WP_Query object though I cannot see how. This seems like a bug in WP unless I’m missing something.

    I did notice the filter in elements.php and added this code which keeps the ‘posts_where’ from running:

    add_filter( 'generate_elements_custom_args', function( $args ) {
    	$args[ 'suppress_filters' ] = true;
    	return $args;
    } );

    Suppressing the filters solves the problem for me, but it is still baffling to me why reseting the query in elements.php did not solve it. Very strange but maybe I’m missing something.

    #1631643
    Tom
    Lead Developer
    Lead Developer

    Very strange, indeed. Perhaps it’s a bug when using get_posts() vs. building our own WP_Query.

    Glad that suppressing the filters solved it for you. We aren’t able to do that by default, unfortunately, as setting it to false is required for multilingual plugins to work.

    #1643337
    Samuel

    In case it helps someone else, I ended up finding a better solution. Instead of depending solely on is_main_query() and is_search() to check a query (which wasn’t reliable), I added another check on the query object:

    if ( empty( $query->query_vars[ 's' ] ) ) {
    		return;
    }

    This checks the query object to see if a search query var has been set. This var is only set on the actual search query so it provides a definitive check as to whether the present query is a search.

    #1644453
    Tom
    Lead Developer
    Lead Developer

    Awesome – thanks for sharing! ๐Ÿ™‚

Viewing 7 posts - 16 through 22 (of 22 total)
  • You must be logged in to reply to this topic.