Question:
How do I adjust the WixData.query and WixData.get functions to search among both visible and hidden items in a collection connected to a dynamic page? (Beginner Friendly)
Product:
[Wix Editor]
What are you trying to achieve:
[I have items in a collection. Some are published and some are in draft mode. I want to use the get and query functions to see if an item exists in a collection, and it seems if the item is marked “hidden” or “hidden/scheduled” in my collection, it marks the search as false. the functions cannot find or pull the item from the collection, which suggest the default functionality of the get and query functions is to search among items marked as “visible.” I want to manipulate the functions to search among and be able to pull both the hidden and visible items in a collection. ]
What have you already tried:
[(1) I’ve read the documentation on CMS, dynamic pages, wix data get, and wix data query. I either can’t find or understand any mention of this situation.
(2) I looked at data hooks, but it seems the before get and before query hooks manipulate the input value that goes into the same query or get functions. They do not affect how the function works by default, which is only searching among published or visible items in a collection, rather than searching among both visible and hidden items in a collection.
(3) Someone in another forum mentioned that you could employ custom logic where you are querying and displaying collection items, but they haven’t said how to do that. I tried something like this:
customQuery(‘Name of Collection’, itemId)
.then((results) => {
if (results.length > 0) {
console.log(‘Found Item:’, results[0]); // Log the found item
} else {
console.log(‘Item not found.’);
}
})
.catch((err) => {
console.log(err);
});
// Custom function to query both visible and hidden items
function customQuery(collectionName, itemId) {
return wixData.query(collectionName)
.eq(‘_id’, itemId) // Filter by the ID of the item
.eq(‘_publishedStatus’, ‘PUBLISHED’) // Include only published items
.or(
wixData.query(collectionName).eq(‘_publishedStatus’, ‘DRAFT’) // Include draft items as well
)
.find()
.then((results) => {
return results.items;
});
}"
this didn’t work either. It seems it is filtering out hidden items before it applies any additional query rules.
Note: I don’t have 100% understanding of these concepts so there could be a configuration here that could still work. ]
Additional information:
[This is important because if I am trying to add or update an item in a collection, and it does not recognize hidden items in the collection, there is a danger of creating duplicates. Your help is appreciated! Thanks! ]