Ecommerce Shopify WordPress Discussion

Alpine js filterPosts is not defined

Im building a WP post filter based of https://joeyfarruggio.com/wordpress/posts-category-filter/ Ive gotten though the lesson but can not solve the final bug, im getting Uncaught ReferenceError: posts is not defined at [Alpine] posts (eval at (cdn.min.js:5:665), :3:32) at cdn.min.js:5:1068 at fr (cdn.min.js:1:6642) at cdn.min.js:5:30943 at r (cdn.min.js:5:16370) at Object.Zr [as effect] (cdn.min.js:5:16158) at N (cdn.min.js:1:398) at cdn.min.js:1:511 at Function. (cdn.min.js:5:30936) at r (cdn.min.js:5:2273) [Alpine] posts @ VM3507:3 (anonymous) @ cdn.min.js:5 fr @ cdn.min.js:1 (anonymous) @ cdn.min.js:5 r @ cdn.min.js:5 Zr @ cdn.min.js:5 N @ cdn.min.js:1 (anonymous) @ cdn.min.js:1 (anonymous) @ cdn.min.js:5 r @ cdn.min.js:5 n @ cdn.min.js:5 ir @ cdn.min.js:5 S @ cdn.min.js:1 (anonymous) @ cdn.min.js:1 Jt @ cdn.min.js:1 (anonymous) @ cdn.min.js:5 setTimeout (async) te @ cdn.min.js:5 (anonymous) @ cdn.min.js:5 Promise.catch (async) (anonymous) @ cdn.min.js:5 fr @ cdn.min.js:1 (anonymous) @ cdn.min.js:5 r @ cdn.min.js:5 Zr @ cdn.min.js:5 N @ cdn.min.js:1 (anonymous) @ cdn.min.js:1 (anonymous) @ cdn.min.js:5 r @ cdn.min.js:5 n @ cdn.min.js:5 ir @ cdn.min.js:5 S @ cdn.min.js:1 (anonymous) @ cdn.min.js:1 Jt @ cdn.min.js:1 (anonymous) @ cdn.min.js:5 cdn.min.js:1 Alpine Expression Error: filterPosts is not defined Expression: "filterPosts(834)" <button @click="filterPosts(834)" class="text-xl text-slate-800 w-full text-left"> Security Operations Platform te @ cdn.min.js:1 (anonymous) @ cdn.min.js:5 Promise.catch (async) (anonymous) @ cdn.min.js:5 fr @ cdn.min.js:1 (anonymous) @ cdn.min.js:5 o @ cdn.min.js:5 (anonymous) @ cdn.min.js:5 (anonymous) @ cdn.min.js:5 <?php /* Template Name: test */ ?> <html> <head> <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script> <script> Alpine.data("filterPosts", (adminURL) => ({ posts: "", limit: 10, category: null, showDefault: true, showFiltered: false, filterPosts(id) { this.showDefault = false; this.showFiltered = true; this.category = id; this.fetchPosts(); }, fetchPosts() { var formData = new FormData(); formData.append("action", "filterPosts"); formData.append("offset", this.offset); formData.append("limit", this.limit); if (this.category) { formData.append("category", this.category); } fetch(adminURL, { method: "POST", body: formData, }) .then((res) => res.json()) .then((res) => { this.posts = res.posts; }); }, })); </script> </head> <body> <h1 x-data="{ message: 'I ❤️ Alpine' }" x-text="message"></h1> <div class="flex items-start gap-8 alignwide mt-32" x-data="filterPosts('<?php echo admin_url('admin-ajax.php'); ?>')"> <!-- Category Filter Sidebar --> <aside class="w-1/4"> <p class="text-xl font-medium">Categories</p> <?php $categories = get_categories(); if ( ! empty( $categories ) ) : ?> <ul class="list-none px-0"> <?php foreach ( $categories as $category ) : // Skip "Uncategorized" category if ($category->name == 'Uncategorized') continue; ?> <li :class="category == <?php echo $category->term_id; ?> ? 'bg-slate-100' : ''" class="hover:bg-slate-100 my-1 px-4 py-2 -ml-4 rounded-md"> <button @click="filterPosts(<?= $category->term_id; ?>)" class="text-xl text-slate-800 w-full text-left"> <?= esc_html( $category->name ); ?></button> </li> <?php endforeach; ?> </ul> <?php endif; ?> </aside> <!-- Posts Column --> <div class="w-3/4"> <!-- Default posts query --> <div class="grid grid-cols-2 gap-6"> <?php $query_args = array( 'posts_per_page' => 10, 'post_status' => 'publish', 'post_type' => 'post' ); $the_query = new WP_Query($query_args); if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?> <div> <?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'full', array( 'class' => 'm-0' ) ); }?> <p class="text-xl font-medium mt-2"><?php the_title(); ?></p> </div> <?php endwhile; wp_reset_postdata(); endif; ?> </div> </div> <!-- Filtered posts --> <div x-show.important="showFiltered" class="grid grid-cols-2 gap-6" x-html="posts"></div> </div> </body> </html>
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.