Get all products under a category

I’ve recently encountered a project that needs to get all products under a category. Creating my own product filter using ajax. So now I’m going to share to you a simple script on how to filter WooCommerce wp_query loop and retrieve all products under the category you have chosen.

You have to make sure that whenever you’re dealing with taxonomies you have to use tax_query

Tax query has these parameters needed to work but could be more once you started using multiple tax_query or nested tax_query. more info

taxonomy – The custom post type category you’re using. mine is product_cat which is the WooCommerce taxonomy.

field – Whether you’re gonna use slug or post id.

terms – The container array of your selected field.

Small things accumulate,power of tiny habits

Doing something but lost the motivation for it? A goal perhaps? that's something we all have in common. If the goal is too large we quit halfway, we lose motivation, experience burnout etc. I encountered this blogs that change my life and embrace tiny habits a new way to get your goal without feeling frustrated. Just a set of repetition is all you need.

A brief summarization about this blogs is that goals are a visual representation of what you want to achieve you want it but you don't know how to get started. It's overwhelming you. They are long-term and some people want's to achieve theirs in just one day and feel bad whenever they fail on doing so.

Habits are more of a systematic approach to doing your goal a ladder. You work on it daily and you get to improve on it as days gone by. What’s more is that you don’t need to have motivation on it you just need to build it up. Small things accumulate, small tiny steps matter.

Using specific WP_Query

A few weeks ago I encountered a task to retrieve an I.Ds with specific requirements of a hotel post type we're currently building. Instead of using an SQL script, I challenge myself to use WP_Query instead. Using specific WP_Query I was able to retrieve what I want.

Here's a brief introduction to WP_Query, It's commonly used to pull pages, posts and post types which if you wanted to create custom templates for your site. More details here.

Here's the snippet I used:

Using meta_query I was able to narrow than the results. I've able to retrieve events that is greater or equal to my selected date, which is another great alternative for SQL query.

Trigger append elements. How?

Have you ever got the need to trigger append elements? append() is mostly useful when you want to insert content or an element at the end of your target container. The common problems when you trigger append elements is that they are not rendered clickable.

Here is an example of an click() button :

The problem why you can't trigger this dynamically is because click() only applies for elements that already exists.
So you have to bind it to something that already exists such as the document.

Use wp_localize_script , It’s Amazing

Have you ever use wp_localize_script ?I'm going to share you a way to use PHP variable in javascript by using this amazing WordPress functionality. When you did this it enables your PHP code to be accessible on your registered script.

Here is a sample :

3 Important Parameters :

$handler - This the registered script that will handle the PHP variable that we attached to our javascript file.

$name - The name of our PHP variable container.

$data - This is mostly an Array if we want to attach multiple variables.

Example :

Shortcode specific scripts

Shortcode is a  powerful WordPress API, right?It allows us to enhance the front- end area of any website add custom functionality to it. In this snippet, I'll show you how to only run a certain script when you shortcode is in use. Which greatly increase our page speed.