How to Hide Some Dynamic Item Pages using code

Goal: Hide some dynamic item pages
Background: I want to filter out those pages with code. No-code dataset filters can’t be used as this filter requires coded functions.

Question: I’ve achieved this filter on the dynamic list page. Just need it replicated on its dynamic item pages
Working page code on dynamic list page:

import wixData from 'wix-data';

$w.onReady(async function () { 
    let filter = wixData.filter();
    filter = filter.ne("hide", true); //to filter out items whose 'hide' field is true

    //To filter out based on another function
    const memberIds = await getInactiveOrders();
    filter = filter.not(
        filter.hasSome('_owner', memberIds)
        .and(filter.eq('status', ["Premium"]))
    )
    $w('#listingsDataset').setFilter(filter);  
});
Failed attempts as a noob
// Approach 1) Just replacing the dataset name in the above code  
// Approach 2) Adding the following bit, then setting the filter on the item 
$w.onReady(async function () {
  const dataset = $w('#dynamicDataset');  
  dataset.onReady(async () => {
    let currentItem = dataset.getCurrentItem();
  //filter...
// Approach 3) AI 
$w.onReady(async function () {
  const dataset = $w('#dynamicDataset');  
  dataset.onReady(async () => {
    let currentItem = dataset.getCurrentItem();

    if (currentItem.hide === true) {  
      blockItem();
      return;
    }

    const memberIds = await getInactiveOrders();
    if (memberIds.includes(currentItem._owner) && currentItem.status === "Premium") {
      blockItem();
    }
  });
});

import wixLocation from 'wix-location';
function blockItem() {
  wixLocation.to("/not-available"); // Redirect to a page
}

Working in Wix Editor

If you’ve already got the filter working on the dynamic list page, you can apply a similar check inside the dynamic item page’s onReady() function. Basically, run your condition when the page loads, and if the item doesn’t meet the criteria you can redirect the user or hide the page content with wixLocation.to() or by collapsing elements. That way the filter logic stays consistent across both list and item pages.

1 Like

Wonder why this response is temporarily hidden. Anyone have a solution, I’d appreciate it

I’ve already attempted this as shown above. I rechecked the syntax and it worked! Many thanks🙏

Hi, though this worked, as it redirects after the load, it reveals the page for a moment too long. So I’ve tried to do this using routers instead. Could you suggest a solution; my post showing latest attempts- How to Redirect some Dynamic Item pages using Routers