0

I am trying to find the list of available values for name in the search to filter results. So far I have found name="s" is the default name="cat" is for categories and name="post_type" is for post types. I am relatively new to WP and I am finding it hard to know how to find details like this.

Can you please show me where in the codex these options came from?

Other resources are appreciated as well.

1 Answer 1

0

There are two name's:

  • The name query variable
  • The name attribute in an input tag

The Query Variable

name represents the post slug.

For example, I have a blog post on my site:

https://tomjn.com/2018/07/23/deployment-and-wordpress/

It has this post name:

deployment-and-wordpress

I can also visit this URL:

https://tomjn.com/?name=deployment-and-wordpress

I could also find it via WP_Query in code using this query:

$query= = new WP_Query([
    'name' => 'deployment-and-wordpress'
]);

The name="..." attribute on an Input

As for the name parameter in a form input, those are query variables, the same ones passed to WP_Query. These are whitelisted internally, but you can refer to the WP_Query documentation for a full list and explanation:

https://developer.wordpress.org/reference/classes/wp_query/

Note that these docs assume you'll be writing PHP code, you may be better off using a pre_get_posts query to give yourself more options in some situations.

Using other values for the name input field won't have any effect on the main query, unless you write code to read it in and make changes yourself.

Keep in mind that this won't work if your themes template discards the main post query and creates a fresh brand new one of its own. That would be bad practice for numerous reasons, and I would suggest instead using the pre_get_posts filter to change what WP fetches from the database in PHP.

A Note on Permalinks and Pretty URLs

When WordPress sees a pretty URL such as:

https://tomjn.com/2018/07/23/deployment-and-wordpress/

It uses the rewrite rule system to turn it into:

https://tomjn.com/index.php?name=deployment-and-wordpress

Then, once that's been done, it processes the result, passing the parameters that are query variables into a WP_Query, e.g.

$wp_query = WP_Query([
    'name' => get_query_var( 'name' ),
]);
// etc

This is why adding parameters via form inputs or in the URL can change the posts WordPress retrieves, e.g. you can add ?s=test to the end of a category archive to search in just that category, or add an input to change parameters, not just in search but on any WordPress post archive.

A Final Note on The Codex

I would also note, that the codex is old, and was replaced by the dev hub at https://developer.wordpress.org/ many years ago. The Handbook and code reference on that site are far superior.

4
  • Thank you for your answer and clarification
    – Iisrael
    Jan 15, 2020 at 20:46
  • If this answers your question could you mark it as accepted? It's the tick under the voting buttons
    – Tom J Nowell
    Jan 15, 2020 at 22:40
  • I think this is a misunderstanding of the question, even though it's accepted. I believe OP is referring to the fact that in the search form you can add <input type="hidden" name="post_type" value="post"> so that only posts are searched (by adding &post_type=post to the search URL). They want to know what other parameters can be used in combination with s to filter results. Jan 16, 2020 at 1:55
  • I see, I've ammended my answer to take that into account and explain both
    – Tom J Nowell
    Jan 18, 2020 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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