Getting wixdata.get and wixdata.query to search for hidden and visible items in a collection tied to a dynamic page

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! ]

A few questions…

This collection you are working with, is it a custom collection you created OR a wix app collection (like stores)

The “hidden” field. What is the data type, I’m assuming it’s a boolean but you didn’t state specifically.

In the code snippets you shared above, I do not see one where you are attempting to query the field you mentioned as being problematic.

Finally - are you testing this on the backend using functional testing or console logging? Or relying on the UI to know that the items aren’t returned?

If you mean you are using this functionality CMS: Controlling Live Site Item Visibility from Your Collection | Help Center | Wix.com

Then, no - i do not believe you can expect to query these, however - I am happy to propose another solution where you control the visibility via code. Let me know

It is better to create your own field to “hide” items and/or add code to control this type of visibility so that you CAN query / get to these items via code.

For example, you can create a boolean (checkbox / true or false) field called “Visibility”, and when you display this data in a repeater, gallery, etc, you can set the dataset settings to show only items marked TRUE in the “Visibility” column.

The new “hidden” feature that the database collections now have is flawed in this logic. Once it is set to ‘hidden’ you cannot retrieve it. (Same for Wix Stores products that are set to hidden.). The way the Devs developed it was with the logic “if you want item to be hidden from live website, it will be hidden everywhere including the search and filter functions”. There is no way to bypass this, not even as an Admin.

1 Like

Hi @codequeen,

Thanks for responding.

That makes a lot of sense. Is there a way to replicate the schedule feature for items using code then? One where you can select a date and then it can switch the boolean value to visible at that specific date and time?

Hi @amandam,

Thanks for responding. Here are the answers:

Yes, it is a custom collection not a wix app collection.

Yes. It is the CMS live site item visibility feature. It’s connected to the collection settings that ask whether all items should automatically be added as hidden or visible items. In the collection (I use table view) this field gives a visibility check, and a schedule option, where you can choose a date next to the status. So let’s say, I selected “hidden,” then clicked “schedule” and selected a date. Based on what I understand from this feature, on that date, the field will switch to “visible”. Also, If I selected “visible”, then scheduled a date, when the time comes, it would switch to “hidden”. but it seems when this function is toggled to “hidden” it becomes hidden from search and filter functions as codequeen mentioned.

Testing: I was testing on the backend and using the console log to see the query search results.

Yes. please share any workaround codes you have. I want to query all items regardless of visibility, but I would still like the other functions of the CMS feature. Especially the part where the visibility is not just for connected dynamic pages, but any lists, items, and references connected to that item that propagate throughout the website.

Thanks!

Hi @imek2477 ,

So, yes you can replicate the “schedule” function. And there are several approaches that you can take to reach this result. Some are more complex than others and it really depends on the logic flow (and your coding comfort level) that will determine what the best approach for your website will be.

For example, if you simply want to display specific items between 2 dates, here is a tutorial that I have showing how to schedule content: https://www.velo.totallycodable.com/post/create-timed-content-scheduled-content-and-randomized-content

That specific tutorial only shows you how to display content on a certain day of the month, but you can modify the code to display between 2 specific dates. So in your database you would have something like “Display Start Date” and “Display End Date”. You can write some code to compare today’s date and see if it is greater than or less than the start / end dates, to then filter and display the appropriate content in your repeater / gallery.

This means you don’t necessarily have to mark the item as “visible” or not, because the code would already be checking if a particular item is being displayed correctly during the correct “scheduled times” you have in the database.


// some other code before this ......

$w("#upcomingEventsDataset").setFilter(wixData.filter()
               .ge("displayEndDate", today) //greater than or equal to
               .le("displayStartDate", today) //less than or equal to
           )
           .then(() => {

//some more code here ........

This is a great approach because it is simple coding, simple logic and you free yourself from the “hidden” functions that prevent you from querying all data in your database.

The more complex approach is to create a Scheduled Job to run every “hour” or “day”, etc. Then when the code runs, it updates something, or does something. It’s complex and more advanced. A bit unnecessary if the first approach gets you the results you are looking for, to be honest.

1 Like

Thank you! I’ll take a look at this and try to make things work.

No problem! I see you have some answers from @codequeen which will be a good path forward.

Ok running into another question while figuring this out. Can you create a code that automatically sets a filter based on that visibility boolean for every dataset and dynamic page on the website, almost like a default setting?

No, you have to manually code it to every page/dataset that way it performs that filter when someone lands on that specific page that they landed on.

It is not a “one time code” for all pages and all datasets.

In that case, could I add the code to the dynamic page in a way that affects all the datasets on it? My hypothesis is if a code like this is added to the original design of the dynamic page, all the dynamic pages generated in the collection would also have this same code.

If you only have 1 dynamic page, with 1 dataset that connects to 1 repeater/gallery element and there are no other datasets on the page —— then you are done coding.

The code will run each time that page is loaded. Even if you have 100 items in that database with 100 URLs that use that 1 dynamic page —— it will run 100 times.

Having 100 items does not mean you have 100 dynamic pages. It is still just 1 page.

If you created 3 different dynamic pages then you need to code it on each page.

Ah this makes sense. Thank you so much!