0

I have a custom post type of job_listing and a custom_post_type of job_application.

Each job_application post has a parent within the job_listing post_type

So if a job listing for an Office Receptionist has 5 applications, then this post will have 5 children within the job_application post_type all having their post_parent ID as the Office Receptionist post in the job_listing CPT (hope this makes sense?)

Now here's the issue...

A job listing could quite easily have a couple of hundred applications (child posts in the job_application CPT) and we need to be able to search for an applicants name within these applications

There are a couple of routes I could take - such as using WP search or WP_Query (I'm using WP_Query here but both methods seem to be facing the same issue...)

The following works as expected, and returns all the Job Applications for the Job Listing with a post ID of 34806

  $args = array(
    'post_type'   => 'job_application',
    'post_status' => 'publish',
    'post_parent' => 34806,
  );

  $apps = new WP_Query($args); 

The Query Objects begins as so (this is relevant in little bit...)

WP_Query Object
(
  [query] => Array
    (
        [post_type] => job_application
        [post_status] => publish
        [post_parent] => 34806
    )

  [query_vars] => Array
    (
        [post_type] => job_application
        [post_status] => publish
        [post_parent] => 34806
        ...

Now here's the issue - Let's say 'Richard' calls to see how his job application is going, and I need to search within my applications for all applicants with the name of Richard (applicant names are all as the_title in the CPT)

If I try to search like this it fails to recognise any post with the term Richard in the title...

  $args = array(
    'post_type'   => 'job_application',
    'post_status' => 'publish',
    'post_parent' => 34806,
    's'           => 'Richard'
  );

  $apps = new WP_Query($args); 

After banging my head against my desk for a couple of days now, I have noticed the WP_Query Object's query_vars (print_r($apps->query_vars);) is behaving differently now I have added the 's' => 'keyword' parameter to the $args...

WP_Query Object
(
  [query] => Array
    (
        [post_type] => job_application
        [post_status] => publish
        [post_parent] => 34806
        [s] => Richard
    )

  [query_vars] => Array
    (
        [post_type] => Array
            (
                [0] => job_listing
            )

        [post_status] => publish
        [post_parent] => 34806
        [s] => Richard
        ...

By adding the s parameter, we have gone from

[post_type] => job_application

to

[post_type] => Array (
  [0] => job_listing
)

So I'm guessing this is where the issue is lurking - I have searched high and low but this parent / child relationship between two separate post types seems to be an edge-case I desperately need your help on.

(FYI - I thought the issue was somewhere in the CPT set up - this does not appear to be the case but in both cases the post types share the following settings)

'public'              =>  true,
'exclude_from_search' =>  false,
'publicly_queryable'  =>  true,
'capability_type'     =>  'post',

Thank you in advance for your assistance and for taking the time to read this.


Furthers to Tom's comment

WP_Query Object
(
[query] => Array
    (
        [post_type] => job_application
        [post_status] => publish
        [posts_per_page] => 20
        [post_parent] => 34806
        [s] => Richard
        [fields] => ids
    )

[query_vars] => Array
    (
        [post_type] => Array
            (
                [0] => job_listing
            )

        [post_status] => publish
        [posts_per_page] => 20
        [post_parent] => 34806
        [s] => Richard
        [fields] => ids
        [error] => 
        [m] => 
        [p] => 0
        [subpost] => 
        [subpost_id] => 
        [attachment] => 
        [attachment_id] => 0
        [name] => 
        [pagename] => 
        [page_id] => 0
        [second] => 
        [minute] => 
        [hour] => 
        [day] => 0
        [monthnum] => 0
        [year] => 0
        [w] => 0
        [category_name] => 
        [tag] => 
        [cat] => 
        [tag_id] => 
        [author] => 
        [author_name] => 
        [feed] => 
        [tb] => 
        [paged] => 0
        [meta_key] => 
        [meta_value] => 
        [preview] => 
        [sentence] => 
        [title] => 
        [menu_order] => 
        [embed] => 
        [category__in] => Array
            (
            )

        [category__not_in] => Array
            (
            )

        [category__and] => Array
            (
            )

        [post__in] => Array
            (
            )

        [post__not_in] => Array
            (
            )

        [post_name__in] => Array
            (
            )

        [tag__in] => Array
            (
            )

        [tag__not_in] => Array
            (
            )

        [tag__and] => Array
            (
            )

        [tag_slug__in] => Array
            (
            )

        [tag_slug__and] => Array
            (
            )

        [post_parent__in] => Array
            (
            )

        [post_parent__not_in] => Array
            (
            )

        [author__in] => Array
            (
            )

        [author__not_in] => Array
            (
            )

        [facetwp] => 1
        [ignore_sticky_posts] => 
        [suppress_filters] => 
        [cache_results] => 1
        [update_post_term_cache] => 1
        [lazy_load_term_meta] => 1
        [update_post_meta_cache] => 1
        [nopaging] => 
        [comments_per_page] => 50
        [no_found_rows] => 
        [search_terms_count] => 1
        [search_terms] => Array
            (
                [0] => Richard
            )

        [search_orderby_title] => Array
            (
                [0] => wp_posts.post_title LIKE '{996078ef2200d94f9cdfbf332158abaebd24e09e889535b5c4c22d13e69bb91f}Richard{996078ef2200d94f9cdfbf332158abaebd24e09e889535b5c4c22d13e69bb91f}'
            )

        [order] => DESC
    )

[tax_query] => WP_Tax_Query Object
    (
        [queries] => Array
            (
            )

        [relation] => AND
        [table_aliases:protected] => Array
            (
            )

        [queried_terms] => Array
            (
            )

        [primary_table] => wp_posts
        [primary_id_column] => ID
    )

[meta_query] => WP_Meta_Query Object
    (
        [queries] => Array
            (
            )

        [relation] => 
        [meta_table] => 
        [meta_id_column] => 
        [primary_table] => 
        [primary_id_column] => 
        [table_aliases:protected] => Array
            (
            )

        [clauses:protected] => Array
            (
            )

        [has_or_relation:protected] => 
    )

[date_query] => 
[request] => SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.ID FROM wp_posts  LEFT JOIN wp_postmeta espm ON (wp_posts.ID = espm.post_id)  WHERE 1=1  AND wp_posts.post_parent = 34806  AND ( ((wp_posts.post_title LIKE '{996078ef2200d94f9cdfbf332158abaebd24e09e889535b5c4c22d13e69bb91f}Richard{996078ef2200d94f9cdfbf332158abaebd24e09e889535b5c4c22d13e69bb91f}') OR  (espm.meta_key = 'cvl_job_ref' AND espm.meta_value LIKE '{996078ef2200d94f9cdfbf332158abaebd24e09e889535b5c4c22d13e69bb91f}Richard{996078ef2200d94f9cdfbf332158abaebd24e09e889535b5c4c22d13e69bb91f}')))  AND (wp_posts.post_password = '')  AND wp_posts.post_type = 'job_listing' AND ((wp_posts.post_status = 'publish'))  ORDER BY wp_posts.post_title LIKE '{996078ef2200d94f9cdfbf332158abaebd24e09e889535b5c4c22d13e69bb91f}Richard{996078ef2200d94f9cdfbf332158abaebd24e09e889535b5c4c22d13e69bb91f}' DESC, wp_posts.post_date DESC LIMIT 0, 20
[posts] => Array
    (
    )

[post_count] => 0
[current_post] => -1
[in_the_loop] => 
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] => 
[is_preview] => 
[is_page] => 
[is_archive] => 1
[is_date] => 
[is_year] => 
[is_month] => 
[is_day] => 
[is_time] => 
[is_author] => 
[is_category] => 
[is_tag] => 
[is_tax] => 
[is_search] => 1
[is_feed] => 
[is_comment_feed] => 
[is_trackback] => 
[is_home] => 
[is_privacy_policy] => 
[is_404] => 
[is_embed] => 
[is_paged] => 
[is_admin] => 
[is_attachment] => 
[is_singular] => 
[is_robots] => 
[is_posts_page] => 
[is_post_type_archive] => 1
[query_vars_hash:WP_Query:private] => 547db0b03a72d39d0cdfdaa561e2a4a4
[query_vars_changed:WP_Query:private] => 1
[thumbnails_cached] => 
[stopwords:WP_Query:private] => Array
    (
        [0] => about
        [1] => an
        [2] => are
        [3] => as
        [4] => at
        [5] => be
        [6] => by
        [7] => com
        [8] => for
        [9] => from
        [10] => how
        [11] => in
        [12] => is
        [13] => it
        [14] => of
        [15] => on
        [16] => or
        [17] => that
        [18] => the
        [19] => this
        [20] => to
        [21] => was
        [22] => what
        [23] => when
        [24] => where
        [25] => who
        [26] => will
        [27] => with
        [28] => www
    )

[compat_fields:WP_Query:private] => Array
    (
        [0] => query_vars_hash
        [1] => query_vars_changed
    )

[compat_methods:WP_Query:private] => Array
    (
        [0] => init_query_flags
        [1] => parse_tax_query
    )

)
9
  • What's the actual title of the post you're expecting to see? Note that fetching just the ids doesn't do that much for performance here, especially if you're calling get_post or any function that retrieves titles meta post content etc, in that situation you'll be forcing WP to make additional queries rather than relying on WP_Cache and the built-in "Here's something I prepared earlier" blue peter style caching of post objects in the same request – Tom J Nowell Feb 23 '20 at 18:44
  • Thanks fir the reply Tom - IDs were in just for my convenience - you can ignore this bit - main issue is search is not picking any posts with ‘Richard’ in the title – richerimage Feb 23 '20 at 19:07
  • Is the title exactly Richard? Or does it just contain Richard? Is it capitalised differently? It's unclear, there are no hard examples of data in your question to reproduce the issue or eliminate ambiguity – Tom J Nowell Feb 23 '20 at 20:47
  • There are several post containing the word Richard in the test data -=. e.g. Richard Barratt, RICHARD JOnes, richard davies, RICHard, etc The key functionality is to be able to search the titles of all child posts connected to the parent post – richerimage Feb 23 '20 at 20:49
  • If you limit it to just post_type and s does it work? Do you have any plugins or pre_get_posts filters that are active? What are the other query vars past the ... in your code snippet? – Tom J Nowell Feb 23 '20 at 20:53

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.