WordPress search block: partial solution
I have tried lots of #plugins to satisfy my needs of customizing #WordPress search, but nothing covered all my requests, especially for what concerns #accessibility both in front-end and back-end. So, I started to search for it on Google and find some code blocks: 1. a block variation, found in an article where [Kinsta blog]{https://kinsta.com/blog/wordpress-6-1} explained what’s new in WordPress 6.1 2. a developer called [Mark Wilkinson]{https://markwilkinson.dev/code-snippets/wordpress-search-block-to-only-search-in-a-custom-post-type/} built another code snippets where search block for only a post type was based on PHP, without JavaScript and using a custom CSS class on the Search block I wanted to set with a specific post type. Class named such as “post-type–i18npost” Unfortunately, both solutions didn’t work! Tried both modifying the function php file in the theme as the guide recommended, and building a small plugin, with the block-variations JavaScript saved in the /assets/ folder of my current theme.
First fail
After having everything set, I realized nothing worked properly. A first search took me to “no posts found”, the second returned undesired results from the wrong post type, namely “posts” when the instruction was to just give me i18npost contents.
Unexpected help
Here came a sighted person’s help who told me “watch out, the address bar is different when you perform searches from the navbar and from the results page.” I honestly never paid attention to the browser’s address bar!
My sighted co-worker was right, then:
- performed search through the customized search block: address bar returned site-url/?s=keyword&post_type=i18npost
second search, performed by the search box appearing just immediately in the results page: site-url/?s=keyword – without other specific parameters. That convinced me to try further tests by manually reaching the pages using given parameters: what about categories, without bothering myself with post types, what should I do for filtering contents by categories!
How to manage categories
Tried to type the URL [my site] /?s=english&category=international – nothing. category=i18n (the slug), nothing. I have done something wrong, without any doubt. Performing some in-deep research on Google I discovered the trick: the URL parameter for category isn’t category but “cat”, and it returns results if associated to the category’s ID, number associated to it.
Then, not being able to identify a category’s ID by keyboard only (as far as I know you can hover it with mouse to get it), I have used the “Show ID” plugin installed from the repository, which adds an extra-column to the administration panel which shows post types and taxonomies.
Now comes the adventurous part.
They often say:
If the mountain won’t come to Muhammad, Muhammad must go to the mountain.
And that’s what I did!
I took the Block Variation code as Kinsta’s website shared about search function and queryvar, then with my very poor PHP/JS knowledge I created the plugin as follows:
<?php
/*
Plugin Name: category international search
Plugin URI: [site URL]
Description: This plugin should create a search block filtered for International category only
Version: 1.0
Author: Bongo, the coding monkey
Author URI: [my site URL]
/
function internationaleditorassets() {
wpenqueuescript(
'international-block-variations',
gettemplatedirectoryuri() . '/assets/block-variations.js',
array( 'wp-blocks' )
);
}
addaction( 'enqueueblockeditorassets', 'internationaleditor_assets' );
?>
/ JavaScript block-variations.js file follows */
wp.blocks.registerBlockVariation(
'core/search',
{
name: 'international-search',
title: 'international',
attributes: {
query: {
cat: '118'
}
}
}
);
And it seems to work!
Mystery of results
Despite all this effort, I consider the issue still open as the results page still loads the traditional search block, with no variations or filters. Next challenge is to fix this, in a way or the other.
Final note
I shared this experience in the WordPress community forum, but moderators blamed me and archived the discussion saying “please blog on your own site, not here”.
Blog? What? I wasn't blogging, I was sharing my experience as I assumed that a support forum was here to give support, not just to ask for it. And giving help is also sharing our own experiences so that they could be useful for others. Never mind, I learned the lesson and write on those forum ONLY if it's seriously the very last hope I get.
Follow me on main Mastodon account @talksina@mastodon.uno