25 votes
Accepted

How would I get a taxonomy/category list inside a Gutenberg block?

You don't need apiFetch or localization to get a list of all categories. You can do this with the wp.data module: wp.data.select('core').getEntityRecords('taxonomy', 'category'); See the Gutenberg ...
Aaron's user avatar
  • 366
22 votes
Accepted

Why are posts from custom post type not displayed in "category" archive?

You need to hook into the query because the category archive page explicitly only includes the 'post' type and nothing else. function namespace_add_custom_types( $query ) { if( (is_category() || ...
socki03's user avatar
  • 1,932
20 votes

Add custom field to Category

As of Wordpress 4.4, the add_term_meta(), the update_term_meta() and get_term_meta() functions have been added. This means that the code as provided by MxmastaMills can be updated to use a far less ...
Luke Simmons's user avatar
20 votes

add_action hook for completely new post?

I have extensively read the WordPresss core and I have tried it all. wp_transition_post_status(), new_to_publish(), new_{post_type}(), wp_insert_post(). All of these are, after all, unreliable. ...
Lucas Bustamante's user avatar
20 votes

Get the category from an ID of a product?

Option #1 Get all product_cat's using this function global $product; $terms = get_the_terms( $product->get_id(), 'product_cat' ); Offical docs Option #2 If you only need their ids, you can get ...
Unicco's user avatar
  • 308
19 votes

Get Category ID inside Category template

base on my search you must use this: $category = get_queried_object(); echo $category->term_id;
Mahdi Majidzadeh's user avatar
16 votes

How to get category id of current post?

When you use get_the_category() function to get category's data, it return the array of object so you have to access category id by passing array key, like this $postcat[0]->term_id global $post; $...
GKS's user avatar
  • 1,306
16 votes
Accepted

What does the token %1$s in WordPress represent

Read the PHP docs on sprintf(). %s is just a placeholder for a string %d is just a placeholder for a number So an example of sprintf would look like this: $variable = sprintf( 'The %s ran down ...
Howdy_McGee's user avatar
  • 20.6k
16 votes
Accepted

Gutenberg editor add a custom category as wrapper for custom blocks

Digging myself deeper in documentation, I got the following result. There is a way to group your custom blocks around a given category in Gutenberg, and therefore we have the method ...
fefe's user avatar
  • 874
15 votes

WP_Query by a category id and a custom post_type

try this, it's work for me. $args=array( 'posts_per_page' => 50, 'post_type' => 'my_custom_type' 'cat' => $cat_id, ); $wp_query = new WP_Query( $args ); Category Parameters ...
kunal Gauswami's user avatar
15 votes

WP REST API: filter by category 1 AND category 2

Multiple categories can be separated by comma like below http://example.com/wp-json/wp/v2/posts?categories=20,30 hope this helps
Manish Jung Thapa's user avatar
14 votes

Hide specific categories from category widget

I know this post is pretty old, but because I came across the same issue and this post came up higher than one with a solution, I figured I'd add this, which worked for me. Source: http://...
Kevin's user avatar
  • 141
14 votes
Accepted

MySQL Query to Retrieve Category from wp_posts

Figured it out. @belinus is probably the solution for you if you're looking to do this within WordPress. As for just a raw SQL query you can play around with, I found this one, modified it a little ...
bbruman's user avatar
  • 464
12 votes
Accepted

Listing a post's categories and subcategories

Update 1: Thanks to @birgire for suggesting a better way: wp_list_categories( [ 'include' => wp_list_pluck( get_the_category(), 'term_id' ) ] ); Try this in your single.php template: $...
wpclevel's user avatar
  • 3,419
12 votes
Accepted

exclude multiple terms using get_terms() function

With get_terms(), the exclude parameter takes an array of term IDs, so just add the second term to the array: $terms = get_terms( TribeEvents::TAXONOMY, array( 'orderby' =>...
Dave Romsey's user avatar
  • 17.7k
12 votes

How to query only for products with status "in stock" in WooCommerce?

That worked great! Just added this: 'meta_query' => array( array( 'key' => '_stock_status', 'value' => 'instock', 'compare' => '=', ...
user116738's user avatar
12 votes
Accepted

Display random categories on the front page (Finding and Editing Theme Functions)

The real question here is: How do I find "TheirCode" which is responsible for this selection using tools such as firefox dev bar and the actual source? If you are referring to the HTML output/...
Sally CJ's user avatar
  • 39.1k
11 votes
Accepted

Different template for subcategories

The template hierarchy has filters for all types of templates. Here we can use category_template, check if the current category has a parent, and load the subcategory.php file in that case: function ...
Milo's user avatar
  • 78.5k
11 votes

Exclude or Include category ids in WP_Query

As you probably know it, categories are taxonomies. When you use the arguments such as category__in, it will add a tax query to your WP_Query(). So, your situation would be something like this: $args ...
Johansson's user avatar
  • 15.3k
10 votes
Accepted

Add a URL prefix to permalinks of one category of posts only

Suppose articles is slug of the category. 1. Add a custom rewrite rule: add_action('init', function() { add_rewrite_rule('^articles/([^/]+)/?$', 'index.php?name=$matches[1]', 'top'); }, 10, 0); ...
wpclevel's user avatar
  • 3,419
10 votes
Accepted

Exclude a category from WP_Query

Issue 1 In your tax query, you should use NOT IN instead of NOT_IN. That's preventing your tax query from working ( Assuming that the other fields are correct ). Issue 2 In your arguments for ...
Johansson's user avatar
  • 15.3k
9 votes

Best Way to Redirect Category to Page and Hide Category

I don't know for some reason, the add_filter caused error. I used the following one: function my_page_template_redirect() { if ( is_category( 'news-articles' ) ) { $url = site_url( '/news'...
Zhang Buzz's user avatar
9 votes

WP_Query by a category id and a custom post_type

this worked for me. $args=array( 'posts_per_page' => 50, 'post_type' => 'my_custom_type' 'tax_query' => array( array( 'taxonomy' => 'category', //double check your ...
Ritchie's user avatar
  • 191
9 votes
Accepted

How do I get the category URL from get_the_category?

Use: get_category_link( $category_id ); See: https://codex.wordpress.org/Function_Reference/get_category_link In your specific case: <?php global $post; $categories = get_the_category(); ...
Adam's user avatar
  • 16.4k
9 votes

How to add featured image for category (without a plugin)?

I had missed that you can easily add fields to taxonomy terms with Advanced Custom Fields: https://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/
BethA's user avatar
  • 311
9 votes
Accepted

Hide uncategorized products from the shop page

I solved this problem based on code kindly provided by rynoldos (https://gist.github.com/rynaldos/a9d357b1e3791afd9bea48833ff95994) as follows: Include the following code in your functions.php file: ...
Clinton's user avatar
  • 851
8 votes

Main menu not appearing on category pages

I know this question is quite old but since there is no answer yet an there are many similar questions on the WordPress support forums, I'll better share my findings and maybe help someone. The issue ...
AndyDeGroo's user avatar
8 votes

How do I remove "Uncategorized" from posts with more than 1 category?

With wp-cli installed you can run a bash script like this to remove the 'uncategorized' category from all posts with more than one category #!/bin/bash for post in $(wp post list --field=ID) do ...
Simon Cossar's user avatar
8 votes

WP REST API: filter by category 1 AND category 2

@Jesse see: WP-API/WP-API#2990 Since WP 4.7, filter has been removed from WP-API. You need to use this plugin: https://github.com/WP-API/rest-filter
Austin Passy's user avatar
8 votes

Exclude a category from WP_Query

https://codex.wordpress.org/Class_Reference/WP_Query category__not_in (array) - use category id. $argsQuery = array( 'posts_per_page' => 3, 'post_type' => 'download', '...
Joe's user avatar
  • 91

Only top scored, non community-wiki answers of a minimum length are eligible