Wordpress Taxonomy help

Hello

I am trying to insert the value of a taxonomy using php. In the past I used a custom field for this but I am switching to a taxonomy. This was my code when using the custom field which works fine:

$name = wcpt_get_name( $product ) . ‘’ . get_post_meta( get_the_ID(), ‘Province’, true ) . ‘’;

The custom field was called “province”. Now my (custom) taxonomy is called “country”. How do I use it instead of the custom field?

Thank you!

To be clear, it is only this part that needs changing, the rest may stay the same:

. get_post_meta( get_the_ID(), ‘Province’, true ) .

https://codex.wordpress.org/Function_Reference/get_taxonomies

I saw this but don’t understand it… It probably has a lot of code I don’t need… I just need to edit the line:

. get_post_meta( get_the_ID(), ‘Province’, true ) .

So that instead of the custom field “province” it looks up the taxonomy “country”.

I tried this:

. get_taxonomies(country) .

And it displays “array” everywhere (no clue what that means but pretty sure it is not correct)…

I also got a feeling that “get.taxonomies” gives you the name(s) of the taxonomies you have (in my case “country”) and not the values from within that taxonomy? I have no clue about php but that is my gut feeling when reading that code on the wordpress codex page.

EDIT:

I tried this:

<?php $args=array( 'name' => 'country' ); $output = 'objects'; // or names $taxonomies=get_taxonomies($args,$output); ?>
private function get_product_name( $product ) {
	$name = wcpt_get_name( $product ) . '</br><i>' . $taxonomy->name . '</i></td>';



	if ( array_intersect( array( 'all', 'name' ), $this->args->links ) ) {
		$name = WCPT_Util::format_product_link( $product, $name );
	}
	return apply_filters( 'wc_product_table_data_name', $name, $product );
}

Throws me errors… I don’t understand why this needs to be so hard… it should just be something like “. taxonomy:country .” or “. taxonomy = country .”

They make these things way to complicated…

How does it have a lot of code you don’t need? It is specifically 1 function that you are after.

I tried this:

<?php $args=array( 'name' => 'country' ); $output = 'objects'; // or names $taxonomies=get_taxonomies($args,$output); ?>

private function get_product_name( $product ) {
$name = wcpt_get_name( $product ) . ‘’ . $taxonomy->name . ‘’;

  if ( array_intersect( array( 'all', 'name' ), $this->args->links ) ) {
     $name = WCPT_Util::format_product_link( $product, $name );
  }
  return apply_filters( 'wc_product_table_data_name', $name, $product );

}

Throws me errors… I don’t understand why this needs to be so hard… it should just be something like “. taxonomy:country .” or “. taxonomy = country .”

They make these things way to complicated…

What are you trying to get out of the call. Maybe I am looking at this from the wrong perspective and am being less helpful than you need.

That is because the call returns an array and you are not parsing what you want from it.

That wouldn’t work anyway. You are out of scope. You define $taxonomy outside of the function and attempt to use it within the function.

Hello

Thank you for trying to help! Again my php is very limited. I edit existing things to try and make it work and can usually understand more or less what code is doing but I can’t write code myself.

I’ll try to explain my situation a bit better.

I have a Woocommerce plugin that shows products as a list (in a table) instead of separate boxes. This table has a cell for the “name” of the product. I want to have the content of a taxonomy as a second line under each name on each row.

So let’s say it is now (3 columns/row):

Row 1 : Name 1 | Rating | Add To Cart Button
Row 2 : Name 2 | Rating | Add To Cart Button

I want it to be:

Row1: Name 1 | Rating | Add To Cart Button
Taxonomy Country | |

Row2 : Name 2 | Rating | Add To Cart Button
Taxonomy Country | |

I did get this working years ago with a custom field using this line (in another plugin):
. get_post_meta( get_the_ID(), ‘Province’, true ) .

Now, with the new plugin that does the same thing but is a lot better I want to do the same but this time with a Taxonomy instead of a Custom Field. (This allows me to set up filters based on taxonomy which I cannot do with the custom field)

I found the php file and place where it searches for and displays the content in the “name cell”. Tried it out with just a plain “text” string and it showed “text” as I want it to as second line of each name cell. I then changed the code of the new plugin and added my old code (the . get_post_meta( get_the_ID(), ‘Province’, true ) . part) instead of the test string and again, works perfect… shows the content of the custom field for each row as second line in the name cell…

Now I used a plugin to add a custom taxonomy, called it “country” and edited all my products to have the correct data in that nex taxonomy.

All that remained was to change the line so that it reads the taxonomy instead of the custom field. And here is where I am stuck…

This is the full code for the custom field (works fine). I made bold the part I personally added to this code, the rest is native from the plugin:

private function get_product_name( $product ) {
$name = wcpt_get_name( $product ) . ‘’ . get_post_meta( get_the_ID(), ‘Province’, true ) . ‘;
if ( array_intersect( array( ‘all’, ‘name’ ), $this->args->links ) ) {
$name = WCPT_Util::format_product_link( $product, $name );
}
return apply_filters( ‘wc_product_table_data_name’, $name, $product );
}

Hope this makes things clearer!

Here is something for you to test out…

[php]private function get_product_name( $product ) {
$taxonomies=get_taxonomies([‘name’ => ‘Country’],‘objects’); // Country or country? There is a difference
$data = print_r($taxonomies, 1);
$name = wcpt_get_name( $product ) . ‘

’ . $data . ‘
’;
if ( array_intersect( array( ‘all’, ‘name’ ), $this->args->links ) ) {
$name = WCPT_Util::format_product_link( $product, $name );
}
return apply_filters( ‘wc_product_table_data_name’, $name, $product );
}[/php]

Hello

This does not throw me a error but does something really weird, it dumps a lot of data on every row, no idea where that all is coming from, I attached a screenshot of one row!


I also tried “Country” instead of “country” and then it gives me:

Array
(
)

for each row as result.

Okay so, “country” is the taxonomy in use based on all that data you get back. Notice that it returns the classification, “taxonomy”, as well as the name, and a slew of other data.

I know the name is country, I made the taxonomy and called it that myself. The question is how to use it. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service