Create associated array out of taxonomy terms - php

I'm trying to get associated array out of taxonomy slug and taxonomy name. I have this
foreach (get_terms('taxonomy') as $tax_term) {
$taxonomy_slug = $tax_term->slug;
$taxonomy_name = $tax_term->name;
}
The issue is, that these are just strings glued together, I don't know how to separate them :\ When I print_r them out I get:
term1term2term3term4...
What I need is a way to separate those, and then create array that will look like this:
Array(
['term_1'] => Term 1
['term_2'] => Term 2
...
)
Is this possible?

As per your inputs in comments I have tried to find out the solution. Hope this will helps you.
$terms = array();
foreach (get_terms('taxonomy') as $tax_term) {
$taxonomy_slug = $tax_term->slug;
$taxonomy_name = $tax_term->name;
$exploded = explode($taxonomy_name,$taxonomy_slug);
foreach ($exploded as $termValue) {
if (!empty($termValue)) {
$terms[$taxonomy_name."_".$termValue] = ucfirst($taxonomy_name)." ".$termValue;
}
}
}
echo "<pre>"; print_r($terms);

Related

Display ACF Taxonomy in order of Multi Select order

Using ACF, I have a Taxonomy field with Multi Select and Stylized UI.
I usually have 2 or 3 tax items selected.
I can arrange them using drag and drop. It's wonderful.
On the front end, the array does not reflect the tax order/sorting that I put in place.
Pretty easy code. What am I doing wrong?
<?php
$values = get_the_terms( $post->ID, 'languages' );
if ( $values ) {
echo '<tr class="item"><td>';
foreach ( $values as $value ) {
echo $value->name . '<br/>';
}
echo '</td></tr>';
}
?>
Figured it out.
Because the field saved the correct sorting in the admin field, I knew the order data was saved somewhere. I looked in the database at the post's terms and the associated data. The order was saved there. Instead of using get_the_terms for the loop, I used get_post_meta. That gave me an array with the term IDs in the correct order. Then I got the term name using that ID within a foreach loop. This gave me the taxonomy names in the order from the styled multi-select field.
<?php
$values = get_the_terms( $post->ID, 'languages' );
if ( $values ) {
echo '<tr class="item"><td>';
$values = get_post_meta( $post->ID, 'languages' );
foreach ( $values as $value ) {
foreach ( $value as $item ) {
$term = get_term( $item )->name;
echo $term .'<br/>';
}
}
echo '</td></tr>';
}
?>

How to remove duplicate custom taxonomy?

I am trying to use custom taxonomy in my custom post. Suppose I have 12 custom post in 3 categories. I only need to display each category for one time. I have used the following code. But it repeated the categories.
<?php
$terms = get_the_terms( $post->ID , 'Categori' );
foreach ( $terms as $term ) {
echo $term->name;
}
?>
Please tell me how can I solved the problem.
You can use array_unique() function to remove duplicate values.
But first you need to store your term values in array.
Here is the full code:
<?php
$terms = get_the_terms( $post->ID , 'Categori' );
$c_terms = array(); //Create empty array
foreach ( $terms as $term ) {
$c_terms[] = $term->name;
}
$unique_cat = array_unique($c_terms);
foreach ($unique_cat as $cat) {
echo $cat;
}
?>

Drop all unique title entries in array

I have a database of 'post titles' containing a lot of duplicates, sometimes with more than 4 posts with the same title.
I want to build an array that gets the ID + Title of every duplicate entry.
$titles = array();
$duplicates = array(); //should be multi-dimensional array containing ID and title of each duplicate entry
//while statement
if (!in_array($post_title, $titles)){
array_push( $titles, $post_title) );
} else {
array_push( $duplicates, $post_title) );
}
//end while
The problem with this is that my $duplicates array only contains the 'second' entry, or the 'duplicate' - I want to store both in the same array. How can I do this using something like array_diff or merge or similar?
I.e. If two posts contain the same title, I want both of these to end up in my $duplicates array, with the respective id and title together.
There's the array_unique function too, but I can't quite figure out how to use this in this scenario...
Assuming $titles[] = array( 'id' => integer, 'title' => string ); and also that id:title is a 1:1 mapping
$count = array();
foreach( $titles as $title ) {
if( !isset($count[$title['id']]) ) {
$count[$title['id']] = 1;
} else {
$count[$title['id']]++;
}
}
foreach( $titles as $title ) {
if( $count[$title['id']] > 1 ) {
$duplicates[] = $title;
}
}
if (!in_array($post_title, $titles)){
array_push( $titles, $post_title) );
}
array_push( $duplicates, $post_title) );
//end while
This way you store the original and the duplicates in the $duplicates array.
Alternatively you can use:
$titles = array_unique( $original_array );

Return all values stored in var outside foreach loop

So I assume something is being overwritten but I am unsure as to how to stop this and retrieve all values outside loop. Any ideas?
foreach($gallids as $gallterm)
{
$postterms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"));
$checkmissing = $postterms[0];
print_r($checkmissing); // Check Terms for missing images - works here.
}
print_r($checkmissing); // Check Terms for missing images - not working here
// - seems to be getting last or first val only.
First of all initialize the variable you want to use later on:
$checkmissing = array();
Then inside the foreach append the first entry of the post terms to that array:
foreach($gallids as $gallterm)
{
list($checkmissing[]) = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"));
}
See $checkmissing[], that is what effectively will prevent that you overwrite it. It will append each to the array.
Finally you can output the result after the loop:
print_r($checkmissing);
Note: You should do some additional handling if wp_get_post_terms returns an empty array:
foreach($gallids as $gallterm)
{
$terms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"))
AND list($checkmissing[]) = $terms
;
}
I tried a few of the examples above and they didn't quite work the way I wanted. So I poked around and did a little research, here's how I got it working for me.
In my particular case I needed to grab all the category ID's for a specific post, then return that variable into the WP_Query arguments array.
First, you will need to grab the post terms
$terms = get_the_terms( $post->ID, 'category' );
Next you'll want to initialize the variable you want to use later on:
$cat_terms = array();
Next you'll declare the foreach to get each individual term ID
foreach ( $terms as $term ) {
$cat_terms[] = $term->term_id;
}
Now let's say you want to use return a comma separated list for this $cat_terms variable. We're going to use the 'join' function
$comma_separated_terms = join( ", ", $cat_terms );
Now let's say you want to use this variable to put into you WP_Query loop for say the 'category__in' parameter. We're going to use 'array_values'.
$values = array_values($cat_terms);
The nice thing about this is now we can insert this $values variable into the WP_Query arguments:
<?php global $post;
$query = new WP_Query(array(
'post_type' => 'post_type_name',
'category__in' => $values));
?>
In my particular case, the client wanted some custom post types to display in the sidebar based on the blog posts categories. So I needed to get all the blog post terms and match them up with the terms for the custom post types categories.
Final Code Looked something like this:
<?php
$terms = get_the_terms( $post->ID, 'category' );
$cat_terms = array();
foreach ( $terms as $term ) {
$cat_terms[] = $term->term_id;
}
$values = array_values($cat_terms);
?>
<h3><?php echo $title; ?></h3>
<?php global $post;
$query = new WP_Query(array(
'post_type' => 'custom_post_type',
'category__in' => $values));
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
// Code for loop goes here
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
$checkmissing = array();
foreach($gallids as $gallterm)
{
$postterms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"));
$checkmissing[] = $postterms[0];
print_r($checkmissing); // Check Terms for missing images - works here.
}
print_r($checkmissing); // Check Terms for missing images
// will get all missing images as array
foreach($gallids as $gallterm) {
$postterms = wp_get_post_terms( $gallterm, 'type', array("fields" => "slugs") );
$checkmissing[] = $postterms[0];
}
print_r($checkmissing); //Now this will be a 2d array with all your values..
$checkmissing = array();
$i=1;
foreach($gallids as $gallterm)
{
$postterms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"));
$checkmissing[$i] = $postterms[0];
//print_r($checkmissing); // Check Terms for missing images - works here.
$i++;
}
print_r($checkmissing);

How do I find the largets item in an array with php (specifically, largest taxonomy_id in an array of ids)?

I am trying to return the highest term taxonomy id of a post page or a taxonomy page.
I was sucessful at listing all taxonomy id's like this:
<?php
$terms = get_the_terms( $post->ID , 'mytaxonomy' );
if($terms) {
foreach( $terms as $term ) {
echo $term->term_taxonomy_id;
}
}
?>
This is what I am trying to do (return only the highest ID) (doesn't work):
<?php
$terms = get_the_terms( $post->ID , 'mytaxonomy' );
if($terms) {
foreach( $terms as $term ) {
echo max( '$term->term_taxonomy_id');
}
}
?>
Please help :)
NOTE:
My main goal is to make this work: https://wordpress.stackexchange.com/questions/9562/multi-level-taxonomy-navigation
The max function can probably compare only two terms at a time. Keep track of the maximum term as you go through the loop - maybe:
maximum_term = max(maximum_term,term->term_taxonomy_id);
No need to use the loop at all just apply max to the $terms variable

Categories