Hello and good afternoon
I have a view and like to show the list of all custom type titles we are having in a drop down, on the search form.
Dear People from Toolset
How long we should wait, until we can expect an answer to the chat we had and to the problem we like to solve?
Regards
Hi,
Thanks for writing back.
As we discussed in our chat this is something that Toolset doesn't offer out-of-the-box and hence the workaround will need some testing and research.
I'll do my best to share my findings as soon as this gets completed, ideally within next few hours.
Thank you for your patience.
For a new/different question or concern, you're welcome to start a new ticket.
regards,
Waqar
Hi,
Thank you for waiting.
To add a select/dropdown of post titles, so that visitors can directly navigate to a selected post, you can use this custom shortcode:
add_shortcode('show_custom_title_field', 'show_custom_title_field_fn');
function show_custom_title_field_fn($atts) {
$type = $atts['type'];
if (!empty($type)) {
$args = array(
'post_type' => $type,
'posts_per_page' => -1,
'post_status' => 'publish',
);
$posts_array = get_posts( $args );
if(!empty($posts_array)) {
ob_start();
echo '<select name="post-titles" class="js-wpv-filter-trigger form-control">';
echo '<option value="">-select-</option>';
foreach ($posts_array as $post) {
echo '<option value="'.$post->guid.'">'.$post->post_title.'</option>';
}
echo '</select>';
return ob_get_clean();
}
}
}
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.
In your view, where you'd like to show this select/dropdown field, you can place this shortcode like this:
[show_custom_title_field type="newspaper-title"]
Please note how I've used the required post type's slug "newspaper-title" in the shortcode attribute "type".
Next, you can include the following custom script in your view's "JS editor", to make the page redirect to the post which is selected through this new select/dropdown field:
jQuery(document).ready( function() {
jQuery('select[name="post-titles"]').change( function() {
location.href = jQuery(this).val();
});
});
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
Dear Waqar
Thank You for this code, so far so good. What do I need to change, I assume in the view I need to change something, maybe by adding an additional filter, to show the results when a customer change in the drop down hidden link
to the selected title?
Additional, to the view I have, how can I change the view results, that it showing nothing, respectively only when the customer choosed a filter?
Regards
Dear Waqar
Dear People from Toolset Support
Do You know when I can expect an answer? My client is waiting for a solution.
Regards
Hi,
Thanks for writing back and Saturday and Sunday are my off-days.
When a select/dropdown is available to pick a specific post's title, it doesn't make much sense to use any other search filter from the view's search form. That is the reason, in the last step of my previous reply, I shared some script that would redirect to the selected post, once the selection is made.
I would recommend moving this custom select/dropdown field at the top of the view and include a note for visitors that they can either select the specific post from the dropdown or use the search fields below, to search for other posts that they're interested in.
regards,
Waqar
Dear Waqar
In our situation it makes sense to have as we need it. Those custom post types don't have them single "page" - instead, it should show only the specific choosed "Title" (post) in this page: hidden link
From there the customer can "order" respectively request and order what he need.
We like to use this dropdown, if a customer already know what "newpaper" he like to order. So he not need to do all search first.
I am sure it is possible to do it, I just don't know how to manage the "search" result.
By the way, how can I order the dropdown? A-Z?
Regards
Thank you for sharing these details.
To make this custom select/dropdown field, work with the search form, you can follow these steps:
1. Please update the code for the custom shortcode from the earlier message to:
add_shortcode('show_custom_title_field', 'show_custom_title_field_fn');
function show_custom_title_field_fn($atts) {
$type = $atts['type'];
if (!empty($type)) {
$args = array(
'post_type' => $type,
'posts_per_page' => -1,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
);
$posts_array = get_posts( $args );
if(!empty($posts_array)) {
ob_start();
echo '<select name="post-titles" class="js-wpv-filter-trigger form-control">';
echo '<option value="">-select-</option>';
foreach ($posts_array as $post) {
if($_GET['post-titles'] == $post->post_title) {
$select = 'selected';
} else {
$select = '';
}
echo '<option value="'.$post->post_title.'" '.$select.'>'.$post->post_title.'</option>';
}
echo '</select>';
return ob_get_clean();
}
}
}
2. Next, instead of adding the custom script from the earlier message, you can add a custom function that filters "wpv_filter_query", to use the selected post's title as a view's search:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )
add_filter( 'wpv_filter_query', 'filter_emp_date_custom_fn', 1000 , 3 );
function filter_emp_date_custom_fn( $query_args, $view_settings ) {
if ( (!is_admin() && isset($view_settings['view_id']) ) && ( $view_settings['view_id'] == 1310 ) )
{
if( (isset($_GET['post-titles'])) && (!empty($_GET['post-titles']))) {
$query_args['s'] = $_GET['post-titles'];
}
}
return $query_args;
}
Note: this code can be added below the custom shortcode's code too.
regards,
Waqar
My issue is resolved now. Thank you!