Tell us what you are trying to do?
I want to use a Google search on certain CPT pages. I want that search to run automatically on page open. My understanding is I can do that by passing the search query through the page URL then the Google search will run using that query. So I need to add the custom field to the url. Am sure it’s simple but can’t find in your docs how to do that?
Is there any documentation that you are following?
Is there a similar example that we can see?
What is the link to your site?
My understanding is I can do that by passing the search query through the page URL then the Google search will run using that query. So I need to add the custom field to the url. Am sure it’s simple but can’t find in your docs how to do that?
Hello, getting the value of a custom field is certainly simple. You can use our Types field shortcode or the Types field PHP API to get the value of any custom field. Shortcode:
Querystring: [types field="your-field-slug" output="raw"][/types]
Types Field PHP API:
$field_value = types_render_field("your-field-slug", array( 'output' => 'raw') );
https://toolset.com/documentation/customizing-sites-using-php/functions/
What you do with this information is more or less complicated depending on what exactly you want to accomplish from a technical perspective.
- Do you want to programmatically adjust internal inbound links to these posts, e.g. internal links from a View or WordPress Archive? That would trigger the custom search immediately upon page load when the User clicks one of these links, but it does not help if the User clicks a link from an external site because the querystring does not already exist in the destination URL on these external inbound links.
- Do you want to programmatically adjust the internal CPT post permalinks using some kind of PHP filtering? Again, this would not trigger the custom search for external inbound links, where the link destination URL does not include the querystring, but it would help adjust links in Views, WordPress Archives, and any other areas of the site where there are links to these posts.
- Do you want to programmatically test for this querystring when the page loads, apply it to the URL programmatically if it does not already exist there, and programmatically reload the page to trigger the search process? This would trigger the search automatically regardless of the inbound link URLs, but is considerably more complex and requires both JavaScript and PHP programming.
- What is the format of the querystring? How does it change from post to post - are there URL parameters that change, or is it a single param with different values per post? Examples?
Hey Chris,
I can expand on the use case maybe that will help.
I am building a website for tying fishing flies. I have CPT for the flypatterns. Internally for the moment when someone navigates to any of the flypatterns post type pages I want the page to open with a number of google searches that 'autoload' based on the fly name (Post_title). One example is images. I want an area of the page to autoload with a google search showing the top images searches for that fly name. (I can control much of the search and its format using the Google Programmable search)
From Googles documentation, I can create a custom search element that searches on page load by having the query as an argument in the page URL (they also say it can be done programmatically but am finding that too complex - me=amateur). So I want to pass the post_title as a search argument (something like &gsc.q="post_title") to my Google Programmable search element via the URL. My thinking being when page loads the search will complete. In due course, I intend to have a google youtube search and possible text search all of which could use that URL argument as a basis for their search.
So I believe I want your first suggestion. I did play around with an addon that let me adjust permalinks but it insisted I have a "/" between each argument and that wouldn't work. I'd be open to that as an option since the post title won't change.
I've read a number of your solutions on here and am impressed with how elegant and simple your solutions can be. I am all for simplicity too!
Thanks for your help
The first solution doesn't require any permalink manipulation, instead it changes the link URLs by changing the link tag markup. To adjust links from a View or WordPress Archive, you would create custom HTML link tags with the search queries applied to the destination URL. For example, if you have a custom field with the slug "searchquery" that holds a querystring like &gsc.q="post_title", you replace the link to each post in your View or WP Archive loop with a custom HTML block that holds this code:
<a href="[wpv-post-url][types field='searchquery' output='raw'][/types]">[wpv-post-title]</a>
That would produce a link to the post with the querystring added at the end of the URL. The text of the link would be the post title. No permalink manipulation is needed in the individual posts.
Thanks Chris I'll take a look at this. In the meantime, I had looked at the settings IN your Edit Post Type page for a CPT and saw two settings that I was wondering if I could use for the same effect. First the 'quer_var'. Could I use this to add the query onto the end of the URL? Not sure it would allow me to add in the "&gsc.q=" or whatever I would need. And likewise the Rewrite setting, which lets you adjust the permalink could that be used in a similar way. I am realizing that every CPT will need this query in the URL so was wondering if i could set it here and not have to adjust any view/loop links at all?
thanks
I don't think you'll be able to use either of these exactly like you want. The query_var setting allows you to specify a query var for querying this post type:
https://developer.wordpress.org/reference/functions/register_post_type/#query_var
As far as I know it does not allow you to append additional query variables to the URL.
The rewrite custom URL format setting allows you to set a different slug for the post type, but does not allow you to append additional URL parameters.
I think you would use something more like this example, with the post_type_link filter:
https://wordpress.stackexchange.com/a/139884