How to Hook: beforeQuery on Blog/Posts collection

Hello, I have a wix site which has the blog app and I want to filter all the blog/posts displayed in my site by a “single condition”.

Imagine that I want to only display the POSTS where the title starts with “Shop.”

If post title is " Shop this guitar" it will be displayed.
If post title is “Play guitar”, it will not be displayed.

I’m a developer, but I’m struggling so much with corvid haha it seems that I do not know nothing.

In regular coding languages, I would just add a where condition to the database query on posts table.

In this case, I see that we can use Hooks “beforeQuery” or “beforeGet”. Which I’m confused which one, but I’m trying with both just in case.

I added 2 hooks just to test the console.out(“hooks being fired”) but it seems not to work, in the following example nothing is shown in the developer console (F12 nor wix console).


So my data.js is like this:

export function Blog$Posts_beforeGet(itemId, context) {
 //TODO: write your code here...
    console.log('Data hook fired');   
}

export function Blog$Posts_afterGet(item, context) {
 //TODO: write your code here... 
    console.log('Data hook fired');   
 return item;
}

So, my question is:

How can I filter by code the “original” query that wix makes to the blog/posts?.

Imagine that I just want to add a single “AND” condition to the query that wix does to the blog/posts.

Can I achieve it by using hooks? Which event?


P.D.: I know you can make a dynamic page with custom feed and load the blog posts, but, there the rich text is not displayed, so, I would like to use the Wix Blog App page but filtering the posts that are displayed by a “custom” query.

Thank you!

It seems to me that you only need a function to set the filter based on the customer input, have you considered setFilter in the dataset as the following :
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFilter

something like :

import wixData from ‘wix-data’;

function filter( name ) {
$w(“#myDataset”).setFilter( wixData.filter()
.startsWith(“title”, name)
)
.then( () => {
console.log(“Dataset is now filtered”);
} )
.catch( (err) => {
console.log(err);
} )
}

Thank you for your help, but I believe datasets only works in dynamic pages, not in the “blog” wix page.

Here I can’t see the “dataset” to apply the filter, because it’s the “wix blog” element.

There is nothing stopping you putting a dataset element on your page and simply connecting that to the Wix Blogs collection.
https://support.wix.com/en/corvid-by-wix/wix-blog-with-corvid

Also, look at this example here.
https://www.wix.com/corvid/example/filter-with-multiple-options

You can use this to work with the Wix Blog as shown in this previous post.
https://www.wix.com/corvid/forum/community-discussion/filtering-blog-posts-to-a-repeater-code-problem

Hello GOS, thank you. I’m trying to pursue your option, which works with repeaters.

I’m, still wondering if I can change the “standard” query of the Blog/Posts collection and add some filters, that would fix my problem from the root.

Thank you for your help

You can see in each collection from the Wix Blog with Corvid link already supplied, that each collection field has a note that tells you if it can be sorted and filtered, plus what filters you can actually use with each field.

You can not change anything from any of the Wix Blog app collections.

To filter blog posts in Wix by a specific condition, like displaying posts where the title starts with “Shop,” you can use the beforeQuery hook in your site’s code.

Here’s how you can achieve this:

import wixData from 'wix-data';

export function Blog$Posts_beforeQuery(query, context) {
    query = query.startsWith('Shop');
    console.log('Hook fired: Filtering posts by title starting with "Shop"');
    return query;
}

In this example:

  • query represents the database query for fetching posts.
  • startsWith('Shop') filters posts where the title starts with “Shop”.
  • Ensure your hook function is correctly set up and registered in your site’s code.

This approach modifies the original query before it executes, allowing you to customize how posts are fetched and displayed on your Wix site.