Skip Navigation

[Resolved] Need help with setting up search for CPTs

This support ticket is created 4 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 44 replies, has 3 voices.

Last updated by jesseH-3 4 years, 1 month ago.

Assisted by: Shane.

Author
Posts
#1777951

Okay the snippet provided by my team leader is essentially the same as the codes I've been using, but he explained that the reason it is not working in some of my tests but is working in others is related to timing. It turns out that code snippets in Toolset > Settings > Custom Code are triggered too late in the WordPress lifecycle to be effective. Instead, the code must be added in a child theme's functions.php file. Here is the full code:

add_filter('register_post_type_args', 'ts_rfg_args', 10, 2);
function ts_rfg_args($args, $post_type)
{

    $rfgs_to_search = array('latest-model-row', 'prev-revision-row'); // Array of slugs of RFGs to include in search

    if (in_array($post_type, $rfgs_to_search)) {
        $args['publicly_queryable'] = true;
        $args['exclude_from_search'] = false;
    }
    return $args;
}

add_action('pre_get_posts', 'ts_include_rfgs_in_search', 999);
function ts_include_rfgs_in_search($query)
{

    $rfgs_to_search = array('latest-model-row', 'prev-revision-row'); // Array of slugs of RFGs to include in search

    if ($query->is_main_query() && $query->is_search() && !is_admin())
    {
        $existing = $query->get('post_type');
        $types = array_merge( $existing, $rfgs_to_search );
        $query->set('post_type', $types);
    }
}

Again, be sure to include this code in a child theme's functions.php file, not in Toolset's code snippets editor. Editing the functions file of the Divi theme is not recommended because every time Divi publishes an update, it will overwrite the changes you have made in functions.php. In my copy of your site you seem to be using Divi without a child theme. If you aren't familiar with creating child themes from scratch, you can find a free one online like Divi Bare Bones: https://www.divithemeexamples.com/bare-bones-divi-child-theme/

#1780201

Thanks Christian. You are correct, the site is not using a child theme (I "inherited" maintenance of the site after it was created). I am not a Divi expert, but with other builder frameworks I've found it difficult to add a child theme after the fact because so many settings are stored in the theme/framework's settings, which is why I've been hesitant to do so. But I'll see what I can do to get switched over to a child theme, and then add the code snippet, and let you know if it solves the issue.

#1780579

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jesse,

Christian is currently out sick today but will return on Sunday to continue assisting with this one.

Thanks,
Shane

#1783221

That's fine, if you find it difficult to implement a child theme you may have better luck with a 3rd-party Code Snippet plugin. Let me know what you find out.

#1793171

I'd like to keep this ticket open but haven't had a chance to take a look at this yet.

#1796047

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jesse,

Has there been any updates on this one ?

Did you get a chance to test out Christian's solution ?

Please let me know.
Thanks,
Shane

#1810593

Thanks for your patience; I have finally returned to this project.
I added the snippet (using third-party plugin Code Snippets) and it works! Searching for GGDADDA gives the proper results.

Now that it's been so long since I've looked into this, and we have a few different test views and pages, I am once again confused about what is needed and what I can delete. Christian, can you clarify which code I should be using and where? Can I use a default WP search field, or do I need to insert a shortcode for a Toolset-specific search? Do I need to use a View, or can I simply use the Search results WP archive? Apologies for being dense, I appreciate your help.

#1811299

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jesse,

Christian is still unavailable so I will be working through this ticket for him.

From what i've been reading you should be using a custom archive for your search results since this will be listing out all your custom post types.

Can I use a default WP search field

Yes you can use the default search page

can I simply use the Search results WP archive?

Yes you can use the WP archive page.

Thanks,
Shane

#1812723

Okay, I think I figured it out except I still am not getting the expected results from items in the RFGs.

Here is what Christian provided for the loop within my search results archive:

[wpv-conditional if="( '[wpv-post-type]' ne 'rfg-slug' )"]
Show this content if the post type slug is not the same as the RFG slug<br />
Title: [wpv-post-title]<br />
Title with link: [wpv-post-link]<br />
Custom field from post: [types field="post-field-slug"][/types]<br />
[/wpv-conditional]
  
[wpv-conditional if="( '[wpv-post-type]' eq 'rfg-slug' )"]
Show this content if post type slug is the same as the RFG slug<br />
Title: [wpv-post-title item="@rfg-slug.parent"]<br />
Title with link: [wpv-post-link item="@rfg-slug.parent"]<br />
Custom field from RFG: [types field="rfg-field-slug"][/types]<br />
Custom field from parent post: [types field="parent-field-slug" item="@rfg-slug.parent"][/types]<br />
[/wpv-conditional]

I have two RFGs and want to search the fields within them both:
RFG 1: latest-model-row
Field A: latest-model
Field B: latest-catalog-number
RFG 2: prev-revision-row
Field A: previous-model-numbers

Can you please tell me how to customize the code Christian provided to include all of these fields? The only output I need for each is the title with link. I've tried a few ways but I get results that are blank or results that link to the RFG or duplicate results. Thanks in advance for your help as this is all pretty over my head.

#1812883

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jesse,

Not sure why you would need to write this in code if you're using a custom archive.

These fields should automatically render if you are creating the custom result page in Toolset -> WordPress Archive.

The code that that christian gave you is to allow for the RFG post type to be included in your default search so these fields should render so long as the RFG post type is being included in your archive search results.

Thanks,
Shane

#1812897

As I understand it, the reason I need custom code for the loop is to properly display RFG search results.
If the loop simply contains [wpv-post-link], the results link to the RFG post instead of the (parent?) post.

#1812903

If I'm doing something wrong, or there is a simpler way to do this, please let me know. I have provided login info in a previous comment.

#1819011

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jesse,

I'm a little confused and need some clarity.

Does the solution here not work or doesn't it display anything new for you ?
https://toolset.com/forums/topic/need-help-with-setting-up-search-for-cpts/page/2/#post-1777951

Let me know what is currently happening.

Thanks,
Shane

#1819037

The code you're referring to fixed my initial issue, which was that no results were being shown when I searched for data in my RFGs.

Now that that is fixed, I need help customizing the code to properly display search results for data within my RFGs. Currently a search for data in an RFG gives a link to the RFG, instead of the post the RFG appears on. Christian seemed familiar with this issue, and provided this code: https://toolset.com/forums/topic/need-help-with-setting-up-search-for-cpts/#post-1764317 which I was able to get to work for one of my RFGs, but I need help customizing that code for both of my RFGs. I've tried to explain this to the best of my ability in my posts on 10/15.

#1821539

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jesse,

Thank you then in this case the code here is appropriate

[wpv-conditional if="( '[wpv-post-type]' ne 'rfg-slug' )"]
Show this content if the post type slug is not the same as the RFG slug<br />
Title: [wpv-post-title]<br />
Title with link: [wpv-post-link]<br />
Custom field from post: [types field="post-field-slug"][/types]<br />
[/wpv-conditional]
   
[wpv-conditional if="( '[wpv-post-type]' eq 'rfg-slug' )"]
Show this content if post type slug is the same as the RFG slug<br />
Title: [wpv-post-title item="@rfg-slug.parent"]<br />
Title with link: [wpv-post-link item="@rfg-slug.parent"]<br />
Custom field from RFG: [types field="rfg-field-slug"][/types]<br />
Custom field from parent post: [types field="parent-field-slug" item="@rfg-slug.parent"][/types]<br />
[/wpv-conditional]

What you need to do is to replace rfg-slug with the slug of the repeatable field groups.

However i'm assuming that you want to display the contents

If you want to display the title of the post that the repeatable field group is attached to you will use this code here.

Title: [wpv-post-title item="@rfg-slug.parent"]

Right replacing the rfg-slug with the correct repeatable field group slug. Example

[wpv-post-title item="@prev-revision-row.parent"]

Please let me know if this helps.
Thanks,
Shane