69

Your tax query is incorrect, field should be the field you want to query on: term_id, name, or slug - $posts_array = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'fabric_building', 'tax_query' => array( array( 'taxonomy' => 'fabric_building_types', 'field' => '...


64

Taxonomies, as previously described are a collective noun for the following category post_tag post_format link_category custom taxonomy The first four are built-in taxonomies, while custom taxonomies are taxonomies that are manually created by the user with register_taxonomy. Custom Taxonomies can be hierarchical (like the build-in taxonomy category) or ...


37

On a category page, you can use the function single_cat_title(), or the more generic single_term_title(). These functions pull from the global $wp_query object, via get_queried_object().


26

taken from that very same function: // prior to wordpress 4.5.0 $args = array( 'number' => $number, 'orderby' => $orderby, 'order' => $order, 'hide_empty' => $hide_empty, 'include' => $ids ); $product_categories = get_terms( 'product_cat', $args ); // since wordpress 4.5.0 $args = array( 'taxonomy' =&...


25

Figured it out! The code below automatically lists all categories and each categories posts! $args = array( 'number' => $number, 'orderby' => 'title', 'order' => 'ASC', 'hide_empty' => $hide_empty, 'include' => $ids ); $product_categories = get_terms( 'product_cat', $args ); $count = count($...


22

Here is the code to get category based previous and next links on posts: <?php $post_id = $post->ID; // current post ID $cat = get_the_category(); $current_cat_id = $cat[0]->cat_ID; // current category ID $args = array( 'category' => $current_cat_id, 'orderby' => 'post_date', 'order' => 'DESC' ); $posts = get_posts( $...


20

You will need to use the ID value returned by $category[0]->category_parent and pass it through get_term(). Example: $category = get_the_category(); $category_parent_id = $category[0]->category_parent; if ( $category_parent_id != 0 ) { $category_parent = get_term( $category_parent_id, 'category' ); $css_slug = $category_parent->slug; } ...


19

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 hacky approach. Here is my update of it. There is only one field as I wanted to add a custom title, but it'll work the same for all the fields you want to add. ...


18

EDIT REVISIT NO 2 I have never touched the Transient API, until today when I saw @MikeSchinkel answer in this post. This inspired me to revisit this post once again. After some testing, I came up with the following: Time to execute went down from ~0.07 seconds to ~0.002seconds Database query time went down by about halve With the transient, only 2 db ...


17

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 Handbook entry on Core Data for more details.


16

base on my search you must use this: $category = get_queried_object(); echo $category->term_id;


16

Normally to add fields to the quick edit field, we should use 'quick_edit_custom_box' action hook that is triggered only for custom columns, because core columns are explicitly excluded (see code). If we add a custom column, then it will be shown in the list table, but it doesn't make sense, because the column description is already present. However, we ...


16

Add the following to your theme's functions.php: /** * Tests if any of a post's assigned categories are descendants of target categories * * @param int|array $cats The target categories. Integer ID or array of integer IDs * @param int|object $_post The post. Omit to test the current post in the Loop or main query * @return bool True if at least 1 of ...


15

That is totally normal behaviour. If you want to remove category base you will need to write some custom rewrite rules, not simple rules I must say. From my point of view, removing category base requires a extra job in each request that is worthless because it doesn't provide any advantage for SEO or for better site navigation. Some popular plugins, like ...


14

If you are on a category archive you don't need to query all the posts of the category like Christopher Ross suggested and you can't use get_the_category() like Max Yudin suggested unless you are already in the loop. What you can do is simply get the current category object using get_queried_object() which will hold the post count, ex: $category = ...


14

The WordPress function switch_to_blog() expects an integer as an input parameter. You can read more about it in the Codex: http://codex.wordpress.org/Function_Reference/switch_to_blog Please try this kind of structure instead: // Get the current blog id $original_blog_id = get_current_blog_id(); // All the blog_id's to loop through $bids = array( 1, 2 );...


14

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 the %s', // String with placeholders 'dog', // Placed in the first %s placeholder 'street' // Placed in the second %s placeholder ); Which ...


14

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 block_categories. So with a filter you can extend the default categories with custom ones. Here is my example: add_filter( 'block_categories', function( $categories, $post ) { ...


13

You can't use get_category_by_slug() with a custom taxonomy. You need to use get_term_by(). $category = get_term_by( 'slug', 'military', 'product_cat' );


13

Use get_term_by: $id = 42; if( $term = get_term_by( 'id', $id, 'product_cat' ) ){ echo $term->name; }


13

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; $postcat = get_the_category( $post->ID ); // try print_r($postcat) ; if ( ! empty( $postcat ) ) { echo esc_html( $postcat[0]->name ); } Hope ...


13

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() || is_tag()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) { $query->set( 'post_type', array( 'post', ...


12

Use get_terms with a parent argument. From the Codex page for that function, emphasis mine: parent (integer) Get direct children of this term (only terms whose explicit parent is this value). If 0 is passed, only top-level terms are returned. Default is an empty string. Untested, but this should do it. $categories = get_terms( 'category', ...


12

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: $current_cats = get_the_category(); $current_cats_ids = []; foreach ($current_cats as $cat) { $current_cats_ids[] = $cat->term_id; } wp_list_categories([ '...


11

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 all product_category_ids associated with a specific product, using this function: global $product; $product_cats_ids = wc_get_product_term_ids( $product->...


11

As I have stated in my comments to your question Crappy written plugins always leads to some disaster at some time. In my opinion, delete that plugin and write your own code or find a properly written plugin. There is no use changing the damaged tire while the whole car is a complete write-off :-) Just to start off, never use query_posts Note: This ...


11

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://coffeecupweb.com/how-to-exclude-or-hide-categories-from-category-widget-in-wordpress-sidebar/ //Hide categories from WordPress category widget function ...


11

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/source, then for example on the official Storefront theme demo site, just right-click on the "Product Categories" heading or section and then you can easily ...


10

You should be extending Walker_Category not the main Walker class. class Walker_Simple_Example extends Walker_Category { function start_lvl(&$output, $depth=1, $args=array()) { $output .= "\n<ul class=\"product_cats\">\n"; } function end_lvl(&$output, $depth=0, $args=array()) { $output .= "</ul>\n";...


10

You may want to try: do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids); You can find it under this Docs and the action is located at wp-includes/taxonomy.php add_action('set_object_terms','wpse5123_set_object_terms',10,4); function wpse5123_set_object_terms($object_id, $terms, $tt_ids, $taxonomy){ if($...


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