v2.1.0
* added feature - query strings
v1.1.2
* added functionality to pass custom_data (setting) to ajax item template (acessed in $custom_data) https://prnt.sc/69GKiA3ZuKhh
v1.1.0
* added sort field
v1.0.3
* added before_render_modify_html js method = for modifying the html, e.g: adding group containers
v1.0.2
* multiple instance render issue fix
v1.0.1
* added filter_checkboxes for multiple checkboxes
This contains the php class and the js file.
Check the module files for example codes including raw versions.
'post',
'showposts' => 8,
'no_found_rows' => true,
];
$settings = [
'item_template' => FF_DIR . '/modules/ff-ajax/item-templates/sample.php',
// this will be used to render the items on ajax
// this will have a reference to the $post object in a query loop
'query_args' => $query_args, // wp query args
'initial_query' => true,
// true = run the query(php) on load
// false = will not run the php query, load the initial items via ajax
];
$ff_ajax = new FF_Ajax($settings);
echo '';
// render html & js
$ff_ajax->render();
echo '';
// default settings
$settings = [
'initial_query' => false,
'filter_on_load' => false,
// set this to true if you change the filter field values on load
// for example, pre-select a value on a dropdown filter, it will then run the query applying that filter
'query_on_change' => true,
// set this to false if you don't want to run the query when the filter fields change/selected
// add custom code to run the query, example below
'load_more_text' => 'Load more', // load more button text
'no_results_html' => 'No results found, try different filters', // html to display when the query has no results
];
the php item template would have access to a $post object inside a WP query->posts foreach
$q = new WP_Query($args);
foreach( $q->posts as $post ) {
include 'item-template.php';
}
$settings = [
// common settings
'label' => 'STRING', // set to empty or ommit to hide the label html
'placeholder' => 'STRING', // first option on the select with value = ""
'filter_key' => 'STRING', // useful for having multiple filters for a single meta_key, e.g: for date range = date_from & date_to
'multiple' => BOOLEAN, // true = select multiple options, renders extra indicator html
'indicator_prefix' => 'STRING', // if multiple is true, adds a prefix text to the indicator html
// tax_query specific
'taxonomy' => 'STRING',
'taxonomy_field' => 'STRING', // default: "slug", other options: 'term_taxonomy_id', 'name'
// meta_query specific
'meta_key' => 'STRING',
'meta_type' => 'STRING', // default: "CHAR", change to NUMERIC for numbers and DATE for dates. Check wp meta_query for other options
'compare' => 'STRING', // default: "=", check wp meta_query for other options
];
$ff_ajax->filter_dropdown($settings);
// filter - tax_query
$ff_ajax->filter_dropdown([
'taxonomy' => 'category',
'placeholder' => 'Category',
'exclude' => ['uncategorized'],
]);
// you can also set the 'choices' to customize the options, e.g
$choices = [];
$terms = get_terms(['taxonomy' => 'category']);
foreach( $terms as $term ) {
$choices[$term->slug] = $term->name;
}
$ff_ajax->filter_dropdown([
'taxonomy' => 'category',
'placeholder' => 'Category',
'choices' => $choices,
]);
// filter - meta_query
$choices = [
'format_1' => 'Format 1',
'format_2' => 'Format 2',
'format_3' => 'Format 3',
];
$ff_ajax->filter_dropdown([
'meta_key' => 'format',
'placeholder' => 'Format',
'choices' => $choices,
]);
// sample choices from acf field
$acf_field = get_field_object('field_65c40d5fbc428');
$choices = $acf_field['choices'];
// taxonomy = single select
$ff_ajax->filter_buttons([
'taxonomy' => 'region',
]);
// meta - multiple select
$field = get_field_object('field_65c40d5fbc428');
$ff_ajax->filter_buttons([
'meta_key' => 'size',
'multiple' => true,
'choices' => $field['choices'],
]);
$ff_ajax->filter_checkbox([
'meta_key' => 'featured',
'label' => 'Meta - checkbox - Featured',
]);
different from single checkbox, filter_checkboxes (with es)
$ff_ajax->filter_checkboxes([
'taxonomy' => 'category',
'multiple' => true,
]);
// uses native WP Query with "s" => 'SEARCH_STRING',
$ff_ajax->search_field([
'placeholder' => 'Search...',
'with_clear' => true,
]);
filter_input([
'meta_key' => 'date',
'placeholder' => 'Date from',
'class' => 'filter-date', // for initializing datepicker
'compare' => '>=',
'filter_key' => 'date_from',
]);
// filter - meta_query - date - to
// different filter_key from the date field above
$ff_ajax->filter_input([
'meta_key' => 'date',
'placeholder' => 'Date to',
'class' => 'filter-date',
'compare' => '<=',
'filter_key' => 'date_to',
]);
// Sample JS
?>
';
echo '';
echo '';
echo '
$ff_ajax->sort_dropdown([
'placeholder' => 'Sort',
'choices' => [
[
'label' => 'Latest',
'orderby' => 'date',
'order' => 'DESC',
],
[
'label' => 'Oldest',
'orderby' => 'date',
'order' => 'ASC',
],
[
'label' => 'Title A-Z',
'orderby' => 'title',
'order' => 'ASC',
],
[
'label' => 'Title Z-A',
'orderby' => 'title',
'order' => 'DESC',
],
[
'label' => 'META - Date ASC',
'meta_key' => 'date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
],
[
'label' => 'META - Date DESC',
'meta_key' => 'date',
'orderby' => 'meta_value_num',
'order' => 'DESC',
],
[
'label' => 'META - Price ASC',
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => 'ASC',
],
[
'label' => 'META - Price DESC',
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => 'DESC',
],
],
]);
id .'" class="ff_ajax">';
// ...
echo '';
// ...
echo '
false,
];
$ff_ajax = new FF_Ajax($settings);
echo '';
// ...
echo '';
// ...
echo '';
?>
Before the html items render, useful for adding intro animations or other DOM manipulations
returns the DOM from jquery selector, use this to modify the html markup, e.g: adding extra containers/html
After the ajax query renders the html items
ff_ajax.after_render = function(){
// hide/show clear button after filter change
if( ff_ajax.clear_filters_button ) {
ff_ajax.clear_filters_button.style.display = !ff_ajax.have_filters() ? 'none' : '';
}
}
Select your desired option below to share a direct link to this page.
Your friends or family will thank you later.