64

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' => '...


63

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 ...


36

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().


33

See the Codex's WordPress Taxonomy documentation. WordPress 2.3 replaced the previous categories, post2cat, and link2cat tables with three a more flexible set of taxonomy tables. wp_terms wp_term_relationships wp_term_taxonomy wp_terms- holds the basic information about single terms. term_id bigint(20) unsigned NOT NULL auto_increment, name varchar(200)...


28

You need to create a new loop for that. Here's the code I use for displaying products from a specific category on the home page: <ul class="products"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' ); $loop = new WP_Query( $args ); while ( $...


25

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

You can't just pass the string "parent" to get_categories. You have to pass the ID of the parent. $categories=get_categories( array( 'parent' => $cat->cat_ID ) ); Notice that there are two similar but not equal "get child" parameters that you can use. child_of (integer) Display all categories that are descendants (i.e. children & ...


21

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

Actually, I wrote a plugin for exactly that. http://wordpress.org/extend/plugins/radio-buttons-for-taxonomies/ In the plugin settings you define which taxonomies you'd like to apply this restriction to.


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. ...


17

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

There are 2 points of attack to cover when you are adding custom post type rewrite rules: Rewrite rules This happens when the rewrite rules are being generated in wp-includes/rewrite.php in WP_Rewrite::rewrite_rules(). WordPress allows you to filter the rewrite rules for specific elements like posts, pages and various types of archive. Where you see ...


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

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 ...


14

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


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

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 ...


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

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 );...


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', ...


13

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 ) { ...


12

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 ...


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

I answered my own question, this work for me : <?php $term_list = wp_get_post_terms($id_product,'product_cat',array('fields'=>'ids')); $cat_id = (int)$term_list[0]; echo get_term_link ($cat_id, 'product_cat'); ?> Thanks Mridul Aggarwal for your help


11

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', ...


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 ...


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