Skip Navigation

[Resolved] Custom filters with custom Taxonomy and Post field

This support ticket is created 4 years, 8 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 5 replies, has 2 voices.

Last updated by Jamal 4 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#1617245

Tell us what you are trying to do?
- I want to achieve filter view in PHP function not with toolset other tools because of some restrictions. I've added a function in funtion.php and displaying it on front end.
- First, I want to merge $category_name_array with $andrew_query so that I should not duplicate the query on one page. I wanted to have dropdown option by just one query.
- Second, Why I used two variables $ category_name_array is for showing the post category on front end in dropdown and $andrew_query is showing all the post in post_type =>'team'.
- Third, their will be 2 drop-downs in front end one for filtering from dropdown added in the backend with custom fields. And other is category in the backend that will work same as first dropdown at front end.
- Forth, Both dropdowns work with the combination. ] if one is selected 2nd will change accordingly.

Is there a similar example that we can see?
- https://toolset.com/forums/topic/show-custom-field-in-filter-dropdowns/
- https://toolset.com/forums/topic/filter-view-via-drop-taxonomy/

What is the link to your site?
- Please provide me hidden link so I can upload the link.

My function:

function team_shortcode( $atts ) {
extract( shortcode_atts( array(
'type' => 'team',
), $atts ) );
$args = array(
'post_type' => 'team',
'post_status' => 'publish',
//'posts_per_page' => 100,
'taxonomy' => 'team-category',
'hide_empty' => 0
);
$output="";
$andrew_query = new WP_Query( $args );

//$output.=print_r($andrew_query).'<br><br><br>';

$category_name_array = get_categories(array('type' => 'team', 'post_status' => 'publish', 'taxonomy' => 'team-category', 'hide_empty' => 0));
//$output.='<br><br><br>'.print_r($category_name_array);
$output .= '<select>';
foreach(($category_name_array) as $category){
$catergory_name_related=$category->name;

$output .= '<option>'.$category->name.'</option>';
}
$output .= '</select>';

$output .= '<select>';
// foreach(($category_name_array) as $category){
// $catergory_name_related=$category->name;

$output .= '<option>'.types_render_field( 'country' ).'</option>';
// }
$output .= '</select>';

while ( $andrew_query->have_posts() ) : $andrew_query->the_post();
$post_id=get_the_ID();
$field = get_permalink($post_id);

$output.='<div class="wpb_column vc_column_container vc_col-sm-3">';
$output.='<div class="vc_column-inner">';
$output.='<div class="wpb_wrapper">';
$output.='<div class="mega_team_case">';
$output.='<div class="mega_team_wrap">';
$output.='<div class="member-image">';
$output.=''.get_the_title().'';
$output.='</div>';
$output.='<div class="member-name">'.get_the_title().'<span style="background-color: #27BEBE;">'.types_render_field( 'designation' ).'</span></div>';
$output.='</div>';
$output.='</div>';
$output.='</div>';
$output.='</div>';
$output.='</div>';

endwhile;
wp_reset_query();
return $output;
}

add_shortcode('team', 'team_shortcode');

Hello and thank you for contacting the Toolset support.

I am afraid, I don't understand your request yet. I could not make the link between the tickets you mentioned, and the code sample. The first ticket is about using a term filed in the taxonomy filter. The second is about hiding/showing filters or filter values when no posts on the results use them. And the sample code is about a shortcode [team], that displays 2 select fields and displays all the team posts.
Note that line 9 'taxonomy' => 'team-category', is not a correct query parameter. Maybe you wanted to use tax_query. Check the possible parameters of WP_Query here https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

Can you explain where do you use the shortcode [team]?
Can you elaborate more on the restrictions that enforce you to use PHP that you mentioned in the first point?

Please correct me here if I am wrong, you want to display a filterable list, where filters will only contain the values that correspond to the current result sets. For example, if you filter by custom post type, and none of the returned results is assigned a category "cat1", "cat1" should not appear on your category dropdown. Right?

#1618323

Dear Jamal,

Apologies for misunderstanding earlier. What I'm trying to do here is showing a custom post through toolset.

What I did is made a custom post type first. Second, I made a custom shortcode function in function.php and then displayed it through WordPress text editor by adding simple shortcode. Now I want to add 2 filter working together at run time.

I want to have two Dropdown/Select on front end. When user click on either dropdown option the result of post display accordingly.

One dropdown is actually a category in backend of WordPress and 2nd is Radio custom field.

Can you please help me to achieve this task? whole in this above mentioned function in PHP. You can change the code mentioned above and make it to achieve.

Thank you

#1621185

You still did not explain what are the restrictions that force you to write PHP code/shortcodes instead of using Toolset Views. Because what you are trying can be done by a Toolset view easily. Check this article which explains how to make the filters display only the values that are relevant. For example, if you filtered by the custom post type, and the results do not have a taxonomy term, this term will not be displayed in the taxonomy filter.
In the "Custom search settings" of the view, select "Let me choose individual settings manually" and then on the bottom you can choose to disable or hide the filters that do not have values. Check this screenshot hidden link
https://toolset.com/documentation/user-guides/views/front-page-filters/#2-1-custom-search-settings

Otherwise, please explain why using custom code for this?

#1621581

Dear Jamal,

I've don't that already in View but there is a conflict in my custom JavaScript that is not working with VIEW and this .JS is necessary to use. for other work as well. That's why I'm using this Custom code in PHP.

Can you help me do that.

Thank you

#1621707

Thank you for your feedback.

It would suggest deactivating AJAX on the view to avoid any Javascript conflict with your code and use the Toolset View instead of recreating it from scratch with code.

If you still want to render your page(filters and results) using your own code/shortcode, then you can use Toolset API. Check the following page https://toolset.com/documentation/programmer-reference/views-api/

You can:
- Create a view that will query the posts depending on a query param.
- Query for results using this view and the function "get_view_query_results"
- Looping over the results to build two arrays containing the possible values for the filters.
- Display the filter.
- Looping over the results to display them. You may want to use the function "render_view_template" to use a content template for rendering each result.

I hope this helps. Let me know your feedback.