Automatically Assign Parent Terms When A Child Term is Selected

Hooking into save_post action. add_action(‘save_post’, ‘assign_parent_terms’, 10, 2); function assign_parent_terms($post_id, $post){ if($post->post_type != ‘product’) return $post_id; // get all assigned terms $terms = wp_get_post_terms($post_id, ‘product_cat’ ); foreach($terms as $term){ while($term->parent != 0 && !has_term( $term->parent, ‘product_cat’, $post )){ // move upward until we get to 0 level terms wp_set_post_terms($post_id, array($term->parent), ‘product_cat’, true); $term = get_term($term->parent, … Read more

How to display custom taxonomies in posts?

The easiest way to list terms of custom taxonomy and display them would be to use <?php get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?> For example in the loop, my custom taxonomy is ‘jobs’ list as li <ul><?php echo get_the_term_list( $post->ID, ‘jobs’, ‘<li class=”jobs_item”>’, ‘, ‘, ‘</li>’ ) ?></ul>

What is wp_insert_term “alias_of” arg for?

In the Code Reference it says: ‘alias_of’ (string) Slug of the term to make this term an alias of. Default empty string. Accepts a term slug. This makes a term an alias of another term. Using your example this is how you would use it: wp_insert_term( ‘e’, ‘tax’, array( ‘alias_of’ => ‘a’ ) ); This … Read more

Plugin to restrict non-admin user to existing tags

If you do not already have your tags created, you can use the “Bulk Add Tags” plugin – http://wordpress.org/extend/plugins/bulk-add-tags/ Then to restrict all users except admins from adding new tags via the “New Post” screen, add this code to your theme’s functions.php file: //Hide Post Page Options from all except Administrator if (!current_user_can(‘administrator’)){ function hide_post_page_options() … Read more

tax_query in get_posts() not working?

tax_query takes an array of tax query arguments arrays (it takes an array of arrays) but you are using only single array. The correct code is as following. $uposts = get_posts( array( ‘post_type’ => ‘product’, ‘numberposts’ => -1, ‘tax_query’ => array( array( ‘taxonomy’ => $cat->taxonomy, ‘field’ => ‘slug’, ‘terms’ => array($cat->slug), ‘operator’ => ‘IN’, ) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)