I want to get existing terms by words.
For example: all terms on "products" containing the word "good" will be shown.
So we get: good books, good movies, good toys etc.
Look at this code:
<?php
$args = array( 'hide_empty=0' );
$terms = get_terms( 'products', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$count = count( $terms );
$i = 0;
$term_list = '<p class="my_term-archive">';
foreach ( $terms as $term ) {
$i++;
$term_list .= '' . $term->name . '';
if ( $count != $i ) {
$term_list .= ' ยท ';
}
else {
$term_list .= '</p>';
}
}
echo $term_list;
}
?>
I took it from https://codex.wordpress.org/Function_Reference/get_terms
This code show me ALL the terms from "products", but I want only the terms from "products" containing the word "good".
Once you have your complete array of taxonomy terms, you can filter out terms containing the word "good" using PHP's preg_grep() function.
$terms = get_terms( 'products', $args );
// This will return all terms that contain "good" in some form, as in 'good books' or 'goodness'
$goodterms = preg_grep("/good/", $array);
Related
Im trying to display the current post category name for a custom post types' taxonomy, but exclude one of the categories. I see different solutions in StackOverflow, but I can't seem to get any of them to work for me. Below is the code I am using, which works great, but I don't know how to exclude one ID using it.
<?php $terms = get_the_terms( $post->ID , 'press_category' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'press_category' );
if( is_wp_error( $term_link ) )
continue;
echo $term->name ;
}
?>
Try this
<?php $terms = get_the_terms($post->ID, 'press_category');
foreach ($terms as $term)
{
$term_link = get_term_link($term, 'press_category');
if (is_wp_error($term_link)) continue;
if ($term->term_id != 222)
{
echo $term->name;
}
}
?>
Change 222 in this line to your taxonomy id for exclude
$term->term_id != 222
I need to display all the product tags as a dropdown on main page. I tried the following code but it did not work.
$terms = get_terms( 'product_tag' );
$term_array = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$term_array[] = $term->name;
}
}
It always returns an empty array. Any suggestions? Thanks.
I'm not certain what you mean by dropdown, but will use <select> for this answer.
In your functions.php
function get_some_tags_man(){
$terms = get_terms( array(
'hide_empty' => false, // only if you want to hide false
'taxonomy' => 'product_tag',
)
);
$html = '';
if($terms){
$html .= '<select name="terms" id="someID">';
foreach($terms as $term){
$html .= "<option name='$term->name'>$term->name</option>";
}
$html .= '</select>';
}
return $html;
}
in your theme file:
<?php echo get_some_tags_man(); ?>
Try this, it will also show empty tags.
$terms = get_terms( array( 'taxonomy' => 'product_tag', 'hide_empty' => false ) );
I just tried the code (in the product page and Shop page) and it worked.
Main Page you mean Shop Page? The first page that appear when you enter the site.
The theme i'm using is StoreFront.
Can you show the hook you are using? or the code? Look. This is the complete code I use.
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_product_loop_tags', 15 );
function woocommerce_product_loop_tags() {
global $post, $product;
$tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) );
$idProducto=$product->id;
$product->get_title();
echo $product->get_tags( ', ', '<span class="tagged">' . _n( 'Tag:', 'Tags:', $tag_count, 'woocommerce' ) . ' ', '.</span>' );
$terms = get_terms( 'product_tag' );
$term_array = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo " ".$term_array[] = $term->name;
}
}
}
I have this code that works great to show the subcategory name and link of a specific parent category of my choosing:
<?php
$taxonomy = 'category';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$categories = get_the_category();
$parentid = '6';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = strtr( wp_list_categories( 'title_li=&style=none&echo=0&child_of=' . $parentid . '&taxonomy=' . $taxonomy . '&include=' . $term_ids), array( '<br />' => ' <br /> ' ) );
echo preg_replace( '#\s<br />\s\n$#', '', $terms );
}
?>
Now I want to be able to do the same thing but WITHOUT the code above generating the link automatically. Any ideas?
Use get_categories() instead of wp_list_categories()
$terms = get_categories("child_of={$parentid}&include={$term_ids}");
foreach($terms as $term)
echo $term->name;
How to get all the post types(not the posts) grouped by taxonomies?
Is there any standard wordpress functions?
I want something like this
taxonomy_1 -> post_type_11, post_type_12, post_type_13, ....
taxonomy_2 -> post_type_21, post_type_22, ....
I have written a query to get the taxonomies and the attached post types as arrays
$query = "
SELECT taxonomy, GROUP_CONCAT( DISTINCT `post_type` SEPARATOR ',') AS `post_types`
FROM wp_term_taxonomy
JOIN wp_term_relationships ON wp_term_relationships.`term_taxonomy_id` = wp_term_taxonomy.`term_taxonomy_id`
JOIN wp_posts ON wp_term_relationships.`object_id` = wp_posts.`ID`
/* you can add other conditions here like - AND post_status = 'published' */
GROUP BY taxonomy
";
then I call this query
$global wpdb;
$post_types_by_taxonomies = $wpdb->get_results( $query, OBJECT_K );
now we can loop through the result array
foreach($post_types_by_taxonomies as $taxonomy => $post_types_as_string){
$post_types = explode(',', $post_types_as_string->post_types);
echo '<hr />';
echo $taxonomy;
echo '<br />';
echo '<br />';
foreach($post_types as $post_type){
echo $post_type;
echo '<br />';
}
}
Please check this one i hope this will work for you.
<?php
// get taxonomies terms links
function custom_taxonomies_terms_links(){
// get post by post id
$post = get_post( $post->ID );
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( !empty( $terms ) ) {
$out[] = "<h2>" . $taxonomy->label . "</h2>\n<ul>";
foreach ( $terms as $term ) {
$out[] =
' <li><a href="'
. get_term_link( $term->slug, $taxonomy_slug ) .'">'
. $term->name
. "</a></li>\n";
}
$out[] = "</ul>\n";
}
}
return implode('', $out );
}
?>
I have a custom post type in which I have custom taxonomies setup.
I want to print out the categories(custom taxonomy) that a post is included in, but exclude one. I cannot find a solution to exclude the category though.
Here is my code to output a list of the categories the custom post type is filed under:
<?php the_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ' ); ?>
How do I exclude a specific category?
Thanks.
You could create a function in your functions.php file that calls get_the_terms to return the list of terms as an array and then remove the item you don't want.
Give this a try:
function get_excluded_terms( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
if(!in_array($term->term_id,$exclude)) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$excluded_terms[] = '' . $term->name . '';
}
}
$excluded_terms = apply_filters( "term_links-$taxonomy", $excluded_terms );
return $before . join( $sep, $excluded_terms ) . $after;
}
and then use it like this:
<?php echo get_excluded_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ', array(667)); ?>