Question:
Hello everyone, everything good?
I came to ask for your help. I’m trying to show the URGENT SECTION if there is a post published on the current date that contains the URGENT tag. The tag will be consulted in the “tags” field of the “Blog/Tags” database, the posts are in the “Blog/Posts” database, within the “Blog/Posts” database there is a “tags” field that does reference to the “posts” field that shows the posts that contain the tags. The post’s publication date is in the “Blog/Posts” database, which contains the “publishedDate” field, where the post’s publication date is stored. However, despite having already edited other codes, I still haven’t managed to do it. Someone help me, please!
Product:
Editor Wix
Additional information:
import wixData from 'wix-data';
async function checkForUrgentPost() {
try {
// Get the current date
const currentDate = new Date();
// Format the current date to match the format of "publishedDate" in the database
const formattedDate = currentDate.toLocaleDateString('en-US', {
day: '2-digit',
month: 'short',
year: 'numeric'
}).replace('.', '');
// Format the month abbreviation to match the database format
const formattedMonth = formattedDate.substr(3, 3);
// Replace the abbreviated month in formattedDate
const formattedDateForComparison = formattedDate.replace(formattedMonth, formattedMonth.toUpperCase());
console.log("Formatted Date for Comparison:", formattedDateForComparison);
// Query the "Blog/Tags" database to find tags with the "URGENTE" label
const tagsResult = await wixData.query("Blog/Tags")
.eq("label", "URGENTE")
.find();
console.log("Tags with URGENTE label:", tagsResult);
// If there are tags with the "URGENTE" label
if (tagsResult.items.length > 0) {
// Check each tag to see if it contains references to posts published today
const urgentPostPublishedToday = tagsResult.items.some(tag => {
// Check if the tag has posts and if any of the references to posts match the publishedDate of today
return tag.posts && tag.posts.some(postRef => {
// Format the publishedDate of the post reference to match the format of "formattedDate"
const postPublishedDate = postRef.publishedDate.replace('.', '');
console.log("Post Published Date:", postPublishedDate);
return postPublishedDate === formattedDateForComparison;
});
});
console.log("Urgent Post Published Today:", urgentPostPublishedToday);
// If there are posts associated with the "URGENTE" tag published today, show the "URGENTE" section
if (urgentPostPublishedToday) {
$w("#secaoUrgente").show();
} else {
$w("#secaoUrgente").hide();
}
} else {
// Hide the "URGENTE" section if there are no tags with the "URGENTE" label
$w("#secaoUrgente").hide();
}
} catch (error) {
console.error("Error:", error);
}
}
// Call the function to check for urgent posts
checkForUrgentPost();