wordpress custom search page pagination returns 404

You should use pre_get_post action to alter the search query. It is much better approach and you will avoid unnecesary queries (actually, with your code, WordPress already performed the database query for the current request but you discard it and perform a new database query). add_action( ‘pre_get_posts’, ‘custom_search_query’ ); function custom_search_query( $query ) { // … Read more

Search by Attachment ID

The WP_Query class can match IDs as well as search terms. An idea would be to use the pre_get_posts action to detect if the search term is numeric and, if it is, set the query to work with attachments while passing the search as the ID. function wpse223307_allow_search_by_attachment_id( $query ) { //Only alter the query … Read more

How to add search to menu?

Use the get_search_form filter: add_filter(‘wp_nav_menu_items’,’add_search_box_to_menu’, 10, 2); function add_search_box_to_menu( $items, $args ) { if( $args->theme_location == ‘primary’ ) return $items. get_search_form(); return $items; } add_filter( ‘get_search_form’, ‘custom_search_form’ ); function custom_search_form( $form ) { ob_start(); // get_stylesheet_directory will get // the main directory of the child theme include( get_stylesheet_directory() . ‘/searchform.php’ ); $form = ob_get_clean(); return … Read more

Show search count by post type

You’re essentially running the same query twice, instead of looping over a second query, why don’t you loop over $wp_query? $types = array(); if( have_posts() ) { while (have_posts()) { the_post(); if ( empty( $types[$post->post_type] ) { $types[$post->post_type] = 0; } $types[$post->post_type]++; } wp_reset_postdata(); rewind_posts(); } You’ll notice I added 3 things: The code in … Read more

Localization of WP theme

Wrap the elements of $search and $replace arrays into __() function to get the translated strings: function search_replace_search() { $search_term = esc_attr( apply_filters( ‘the_search_query’, get_search_query( false ) ) ); // Get search term $search = array( __(‘word1’), __(‘word2’) ); $replace = array( __(‘something 1’), __(‘something 2’) ); $replacePairs = array_combine($search, $replace); echo strtr($search_term, $replacePairs); } … Read more

Woocommerce search form with category select

You can try below code. Below code shows search box and woocommerce product categories . It will also show selected category. <form name=”myform” method=”GET” action=”<?php echo esc_url(home_url(“https://wordpress.stackexchange.com/”)); ?>”> <?php if (class_exists(‘WooCommerce’)) : ?> <?php if(isset($_REQUEST[‘product_cat’]) && !empty($_REQUEST[‘product_cat’])) { $optsetlect=$_REQUEST[‘product_cat’]; } else{ $optsetlect=0; } $args = array( ‘show_option_all’ => esc_html__( ‘All Categories’, ‘woocommerce’ ), ‘hierarchical’ => … Read more