Home › Forums › Pro Support › Search Results page
- This topic has 7 replies, 2 voices, and was last updated 1 year, 9 months ago by
Fernando.
-
AuthorPosts
-
March 7, 2022 at 11:12 pm #36705
Gloria
ParticipantHello everyone.
I am using WP Show Post together with Elementor PRO.
On the search results page I want to use WPSP, but I can’t.
How should I prepare the query for it to work?
ThanksMarch 7, 2022 at 11:28 pm #36707Fernando
ParticipantHi Gloria,
Linking Search results to a WP Show Post List isn’t possible.
If you wish to exclude categories in your search results page, you would need a custom code like this: https://wordpress.stackexchange.com/questions/162232/exclude-categories-from-search-query#:~:text=1-,Answer,-Active
Also see for how to get the Category ID: https://share.getcloudapp.com/7KuQ1KKG
You may add this code through a plugin like Code Snippets.
Hope this clarifies! 🙂
March 8, 2022 at 12:29 am #36710Gloria
ParticipantThank you for your reply.
The aim was to have uniformity in the graphics on the search results page as well.
If it is not possible to do this, I will use Elementor’s default settings.
March 8, 2022 at 12:49 am #36712Fernando
ParticipantThough it is not possible through WPSP functionalities, we can add a PHP code like this to effectively link the Search Results page to a WPSP List:
add_action( 'generate_before_main_content', function() { if ( is_search() ) { wpsp_display( 1362 ); } } ); add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 1362 === (int) $settings['list_id'] ) { $args['s'] = esc_html( $_GET['s'] ); } return $args; },20,2 ); add_filter( 'generate_has_default_loop', function( $has_loop ) { if ( is_search() ) { return false; } return $has_loop; } );
Tested this on my test website just now and it’s working. Kindly replace
1362
with the WPSP list ID.You may add this custom PHP code through a plugin like Code Snippets.
Hope this helps! 🙂
-
This reply was modified 1 year, 9 months ago by
Fernando.
March 8, 2022 at 5:24 am #36719Gloria
ParticipantHi Fernando, thanks for your reply.
Unfortunately I’m using the Hello theme, so ‘generate_before_main_content’ and ‘generate_has_default_loop’ don’t work for me.Can you help me anyway?
March 8, 2022 at 5:53 pm #36727Fernando
ParticipantI see. If that’s the case, if you’re using a child them, what you can do is manually remove the default loop in the search template file, and replace manually run the shortcode of the WPSP list.
Example of an edited template-parts/search.php file in Hello theme:
<?php /** * The template for displaying search results. * * @package HelloElementor */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } ?> <main id="content" class="site-main" role="main"> <?php add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 1362 === (int) $settings['list_id'] ) { $args['s'] = esc_html( $_GET['s'] ); } return $args; },20,2 ); if ( apply_filters( 'hello_elementor_page_title', true ) ) : ?> <header class="page-header"> <h1 class="entry-title"> <?php esc_html_e( 'Search results for: ', 'hello-elementor' ); ?> <span><?php echo get_search_query(); ?></span> </h1> </header> <?php endif; ?> <div class="page-content"> <?php echo do_shortcode('[wp_show_posts id="1362"]'); ?> </div> <?php wp_link_pages(); ?> <?php global $wp_query; if ( $wp_query->max_num_pages > 1 ) : ?> <nav class="pagination" role="navigation"> <?php /* Translators: HTML arrow */ ?> <div class="nav-previous"><?php next_posts_link( sprintf( __( '%s older', 'hello-elementor' ), '<span class="meta-nav">←</span>' ) ); ?></div> <?php /* Translators: HTML arrow */ ?> <div class="nav-next"><?php previous_posts_link( sprintf( __( 'newer %s', 'hello-elementor' ), '<span class="meta-nav">→</span>' ) ); ?></div> </nav> <?php endif; ?> </main>
As shown, I ran/added this filter:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) { if ( 1362 === (int) $settings['list_id'] ) { $args['s'] = esc_html( $_GET['s'] ); } return $args; },20,2 );
and manually ran the shortcode of the WPSP list with:
echo do_shortcode('[wp_show_posts id="1362"]');
Kindly replace
1362
with the list’s ID.Tested this with Hello Theme and it seems to be working.
Hope this clarifies. 🙂
March 9, 2022 at 1:53 am #36733Gloria
ParticipantThank you very much for your reply.
I will try as soon as possible.March 9, 2022 at 5:07 pm #36735Fernando
ParticipantYou’re welcome Gloria! Hope it works! Feel free to reach out anytime if you’ll need assistance with anything else. 🙂
-
This reply was modified 1 year, 9 months ago by
-
AuthorPosts
- You must be logged in to reply to this topic.