0

I have issue with ElasticPress.

After successful installation and connection of WordPress and ElasticPress and indexing few hundred posts for testing, i found everything in order until I tried to create custom WP_Query. The result page had only title lines but the post content was not displayed. In the search.php I wrote this

$wp_query = new WP_Query( array(
                    'ep_integrate'   => true,
                    'post_type'      => 'post',
                    'posts_per_page' => 20,
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'category',
                            'terms'    => array( get_query_var('cat') ),
                            'field'    => 'term_id',
                        ),
                    ),
                ) );


                if ( $wp_query->have_posts() ) {

                    // Start the loop.
                    while ( $wp_query->have_posts() ) : $wp_query->the_post();

                        /**
                         * Run the loop for the search to output the results.
                         * If you want to overload this in a child theme then include a file
                         * called content-search.php and that will be used instead.
                         */
                        get_template_part('template-parts/content', 'search');

                        // End the loop.
                    endwhile;

                    // Previous/next page navigation.
                    the_posts_pagination(array(
                        'prev_text' => __('Previous page', 'twentysixteen'),
                        'next_text' => __('Next page', 'twentysixteen'),
                        'before_page_number' => '<span class="meta-nav screen-reader-text">' . __('Page', 'twentysixteen') . ' </span>',
                    ));

                    wp_reset_postdata();

                    // If no content, include the "No posts found" template.
                } else {

                    get_template_part('template-parts/content', 'none');
                }

Unfortunately I didn't find anything about how to correctly use ElasticPress when building custom queries. All tutorials are simple, shows how to write new WP_Query and the results are built without any other programming.

The global post var is not empty, still the post content missing on page. WordPress is 4.6.1, ElasticPress is 2.1.2.

Can someone direct me to the correct way of creating custom queries and display the results. I know ES but I'm almost beginner in WP.

0

use this:

function elasticpress_search( $wp_query ) { 
    if ( is_search()  ) { 
        $query->set( 'ep_integrate', true ); }     
}

add_action( 'pre_get_posts', 'elasticpress_search' );

then add other condition with $query->set

  • Thank you for fast replay. I didn't want to complicate my question but I've already tried with pre_get_posts and the test queries were successful. But what if I want to use the "new WP_Query" to utilize the full power of ElasticPress? – fly Jan 19 '17 at 15:04
  • just use the code posted you, wordpress by default create a main query, so if you want replace entire main query require suppress it with code like this add_filter('posts_request', 'supress_main_query', 10, 2); function supress_main_query( $request, $query ){ if( $query->is_main_query() && ! $query->is_admin ) return false; else return $request; } – themich Jan 24 '17 at 16:16
  • Thanks @themich on help. My conclusion from the ElasticPress docs is that EP hooks WP's WP_Query object and builds the elastic query from parameters in this object. The only way to alter elasticsearch query is to alter arguments in WP_Query. Altering the final query ($request) is without influence on EP. Thus, all I can do from this standpoint is to build new WP_Query like presented in my first post and find how to correct the missing post content values in result list. Maybe I need to send somehow the post id for each row in the loop to print out the post_content values. – fly Jan 25 '17 at 13:32
  • Again, I don't have issues using pre_get_posts action to alter WP_Query object but I want to learn how to build new WP_Query using EP documentation and solve possible advanced search requests. Thats why I need help on issue mentioned in my first post - post content not printed only post title. – fly Jan 25 '17 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.