Hi, I have been trying to replicate the structure of this example;
https://www.wix.com/corvid/example/filter-with-multiple-options
My goal is to have additional pages on my blog website and on these pages, I want to provide a way to filter the posts with multiple categories. I adapted the code in the example to my prototype page, but I keep getting "Unsupported filter operator $hasAll for Categories" error when I click on the filter tags in preview. One thing I don't understand is the "selectedCategories" part of the code. There are no elements labelled categories in the example, so I don't know what it exactly is referring to. My adapted code is below: (It brings blog posts to the repeater but gives an error message when I click filter tags) Any help would be much appreciated.
import wixData from 'wix-data';
const collectionName = 'Blog/Posts';
const fieldToFilterByInCollection = 'Categories';
$w.onReady(function () {
setRepeatedItemsInRepeater()
loadDataToRepeater()
$w('#selectionTags').onChange((event) => {
const selectedSelectionTags = $w('#selectionTags').value
loadDataToRepeater(selectedSelectionTags)
})
});
function loadDataToRepeater(selectedCategories = []) {
let dataQuery = wixData.query(collectionName)
if (selectedCategories.length > 0) {
dataQuery = dataQuery.hasAll(fieldToFilterByInCollection, selectedCategories)
}
dataQuery
.find()
.then(results => {
const itemsReadyForRepeater = results.items
$w('#blogPosts').data = itemsReadyForRepeater;
const isRepeaterEmpty = itemsReadyForRepeater.length === 0
if (isRepeaterEmpty) {
$w('#noResultsFound').show()
} else {
$w('#noResultsFound').hide()
}
})
}
function setRepeatedItemsInRepeater() {
$w('#blogPosts').onItemReady(($item, itemData) => {
$item('#coverImage').src = itemData.coverImage;
$item('#coverImage').tooltip = itemData.title;
$item('#title').text = itemData.title
$item('#excerpt').text = 'By ' + itemData.excerpt;
})
}
I have just tested it myself and it does not work with the Wix Blog app own collection as it is read only.
If you use your own dataset and export the Wix Blog Posts collection data into that new dataset, then it will work.
https://givemeawhisky.wixsite.com/tagstest
import wixData from 'wix-data'; const collectionName = 'test'; const fieldToFilterByInCollection = 'tags'; $w.onReady(function () { setRepeatedItemsInRepeater() loadDataToRepeater() $w('#tags').onChange((event) => { const selectedTags = $w('#tags').value loadDataToRepeater(selectedTags) }) }); function loadDataToRepeater(selectedCategories = []) { let dataQuery = wixData.query(collectionName) if (selectedCategories.length > 0) { dataQuery = dataQuery.hasAll(fieldToFilterByInCollection, selectedCategories) } dataQuery .find() .then(results => { const itemsReadyForRepeater = results.items $w('#blogPosts').data = itemsReadyForRepeater; const isRepeaterEmpty = itemsReadyForRepeater.length === 0 if (isRepeaterEmpty) { $w('#noResultsFound').show() } else { $w('#noResultsFound').hide() } }) } function setRepeatedItemsInRepeater() { $w('#blogPosts').onItemReady(($item, itemData) => { $item('#blogimage').src = itemData.coverImage; $item('#blogname').text = itemData.title; $item('#publisheddate').text = 'By ' + itemData.publishedDate; }) }
The Wix Blog test one is here.
https://givemeawhisky.wixsite.com/tagstest/test
import wixData from 'wix-data'; const collectionName = 'Blog/Posts'; const fieldToFilterByInCollection = 'Hashtags'; $w.onReady(function () { setRepeatedItemsInRepeater() loadDataToRepeater() $w('#tags').onChange((event) => { const selectedTags = $w('#tags').value loadDataToRepeater(selectedTags) }) }); function loadDataToRepeater(selectedCategories = []) { let dataQuery = wixData.query(collectionName) if (selectedCategories.length > 0) { dataQuery = dataQuery.hasAll(fieldToFilterByInCollection, selectedCategories) } dataQuery .find() .then(results => { const itemsReadyForRepeater = results.items $w('#blogPosts').data = itemsReadyForRepeater; const isRepeaterEmpty = itemsReadyForRepeater.length === 0 if (isRepeaterEmpty) { $w('#noResultsFound').show() } else { $w('#noResultsFound').hide() } }) } function setRepeatedItemsInRepeater() { $w('#blogPosts').onItemReady(($item, itemData) => { $item('#blogimage').src = itemData.coverImage; $item('#blogname').text = itemData.title; $item('#publisheddate').text = 'By ' + itemData.publishedDate; }) }
GOS thanks a lot for the answer. The examples you linked, unfortunately, do not provide the filtering I need. I want to to be able to select several options out of many, and end up with an intersection set of all the selected options. The idea is to narrow down the search by selecting additional filters. It works in the example I linked on my first post.
I thought I would be able to use the categories field under the Posts dataset. I also tried adding hashtags to posts and using the hashtags field as a filter. (Did not work.) Both seem to be filterable by hasAll according to the link you provided.
"Finally, note that the categories field in the Wix Blog Posts Collection is actually a reference field from the Wix Blog Categories Collection." I think my problem may lie here, but I don't understand what I need to do. I tried using the Labels from Blog Categories Collection, but that also did not work.
These are the already provided examples for working with the Wix Blog app.
https://www.wix.com/corvid/examples?category=blog
You might also want to use the hashtags field as well.
https://support.wix.com/en/article/corvid-wix-blog-posts-collection-fields#hashtags-hashtags-1
Hashtags (hashtags) Description: List of all hashtags in the post. Type: Tags Can connect to data: No Can use in dynamic page URL: No Can be sorted: No Can be filtered: hasAll, hasSome Read-only: Yes
https://www.wix.com/corvid/reference/$w.SelectionTags.html
Also, I would double check that the fields that you are trying to use are actually allowing that type of data query on it as not all of the fields in Wix apps are filterable etc.
https://support.wix.com/en/corvid-by-wix/wix-blog-with-corvid
Finally, note that the categories field in the Wix Blog Posts Collection is actually a reference field from the Wix Blog Categories Collection.
Categories (categories)
Description: The categories assigned to the post, referenced from the Blog/Categories collection. Type: Multiple-item reference Can connect to data: No Can use in dynamic page URL: No Can be sorted: No Can be filtered: hasAll, hasSome, contains Read-only: Yes