0

I am working on a WordPress site that has several custom post types. Of course this lends itself to writing a custom search form. However, the results from test searches have been less than understandable.

Let me paint a picture of the posts that are being searched.

Posts

  1. Post Type: A, Keywords: apple, bed, all
  2. Post Type: A, Keywords: apple, cat, all
  3. Post Type: B, Keywords: cat, dog, all
  4. Post Type: B, Keywords: dog, egg, all
  5. Post Type: C, Keywords: apple, egg, all

Post Types A, B, and C have 'exclude_from_search' => false and 'publicly_queryable' => true.

Based on https://codex.wordpress.org/WordPress_Query_Vars I would expect that I could use post_type in the search query string to define the post type. However, when I do so the results are not what I would expect.

Queries

  1. ?s=apple results in 1 and 2. It should be 1, 2 and 5.
  2. ?s=apple&post_type=A results in 1 and 2. This is correct.
  3. ?s=apple&post_type=B results in 1 and 2. It should be none.
  4. ?s=apple&post_type=C results in 1 and 2. It should be 5.
  5. ?s=dog results in 3 and 4. This is correct.
  6. ?s=dog&post_type=A results in 3 and 4. It should be none.
  7. ?s=dog&post_type=B results in 3 and 4. This is correct.
  8. ?s=dog&post_type=C results in 3 and 4. It should be none.
  9. ?s=all results in 1, 2, 3, and 4. It should be 1, 2, 3, 4, and 5.
  10. ?s=all&post_type=C results in 1, 2, 3, and 4. It should be 5.

Questions

  1. Does post_type do anything when included in the URL?
  2. Why does 5 never show up in the results? In fact no C post types are shown in search results.
  3. Is this part of WordPress just fundamentally broken, and I should use a pre_get_posts hook to modify the query?

Your Answer

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

Browse other questions tagged or ask your own question.