How to get taxonomy id by it's name? Drupal 9 - drupal

While creating a node, I am required to input entity reference ID in the field_tag.
The problem is that I only have entity reference name.
How can I get the term id (tid) for a taxonomy term from the term name?
I'm very surprised that I can't find an answer anywhere, since this seems to me to be a simple, basic question.
$node = Node::create([
'type' => 'news',
'title' => $naslov,
'body' => $tekst,
'field_tag' => // Taxonomy reference name is "ostalo", I need it's ID here
],
);
$node->save();

The taxonomy_term_load_multiple_by_name function might be deprecated.
Check here.
Consider using something like:
$vid = 'tags';
$term_name = 'ostalo';
$terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadByProperties([
'vid' => $vid,
'name' =>$term_name,
]);
foreach ($terms as $term) {
$term_id = $term->id();
}

You can use the taxonomy_term_load_multiple_by_name function.
eg.
$tids = taxonomy_term_load_multiple_by_name('ostalo');
EDIT..
Woops, didn't realize I was looking at an older version of the api. As Balde Binos says, it's deprecated. His answer is the correct one.

Related

Wordpress show all Custom Post Type except one

I am using this loop:
<?php
$search_count = 0;
$search = new WP_Query("s=$s & showposts=-1");
if($search->have_posts()) : while($search->have_posts()) : $search->the_post();
$search_count++;
endwhile; endif;
echo $search_count;
?>
How can I say, that I want to show every Custom Post Type, except for the post type called 'klanten' (clients in Dutch)?
You can use like this:
$args = array(
's' => $s,
'posts_per_page' => -1,
'post_type' => array( 'post', 'page', 'movie', 'book') // include the posts type you want to show
);
$query = new WP_Query( $args );
Unfortunately, there's currently no way you could exclude a specific post_typeby defining it. So, the workaround is you only define the post_type you want to include.
A couple of years too late, but you can use the get_post_types() function with the operator parameter set to not. This way, you will receive an array of all post types except the one(s) you set in the args parameter of the function, which you can then use to define the post_type in your query:
$args = array(
's' => $s,
'posts_per_page' => -1,
'post_type' => get_post_types(array('name' => 'klanten', 'public' => false), 'names', 'not')
);
$query = new WP_Query( $args );
However, you might want to var_dump() the result of the function first, since it will return other hidden post types you might not be interested in (like 'attachments' or others defined by plugins). This is the reason why I added 'public' => false, which will make sure that I only receive public post types. This removes most of the undesired post types, but you should still check ('attachments' is not removed by this).
Another shortcoming of this method is that you can only remove one post type. A workaround for that would be to get all post types and then remove the ones you don't want from the array using the method described here, but that seems like too much code, so in that case you might be better off just setting all the post types you want instead of the ones you don't.

Wordpress query_posts with ID and slug combination

is it possible to use query_posts with a combination of id's and slugs?
I have an array of id's and slugs from user input and want to know if I can use the two in the same post query, or do I have to convert the slugs into their respective post ID's before and then use posts__in?
I have the following mixture of slugs and ID's in an array…
$values = array('this-is-a-test-slug', '2345', '4510', 'another-slug-here', '8934');
How can I use this in query_posts? Can I at all?
query_posts(array('post_type' => 'post', 'post__in' => $values, 'orderby' => 'rand'));
I know that post__in works ok with numeric ID's but I don't think slugs work here as it expects a numerical array.
Thanks
If you're doing something like this, i don't see why it wouldn't be a problem to just convert them all over to ID? There's a thread that sort of helps, and I've written some code (with help from that link) that might help you get started
function allToID($array){
$id = array();
foreach($array as $convert):
if(is_numeric($convert)):
continue; // skip if it's already an ID
else:
$the_slug = $convert;
$args=array(
'name' => $the_slug,
'numberposts' => 1
);
// Ask wordpress to get this post
$my_posts = get_posts($args);
if( $my_posts ) :
// push onto our new array of only IDs
array_push($id, $my_posts[0]->ID);
else continue;
endif;
endif;
endforeach;
}
Ideally you'll be able to run post__in => alltoID($values)
Hope this helps!

Drupal fields: Passing a variable to view

I have a custom field plugin. It's widget looks somewhat like this:
function mymodule_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
...
$main_widget = $element + array(
'#test' => 'test',
'#type' => 'textarea',
'#attributes' => array('item_capacity' => 3),
'#theme'=>'mymodule_theme'
);
...
}
I need to get a value of another field, attached to current node, and pass it to this widget's theme file. Can i somehow get at least an id of a node that contains current field from hook_field_widget_form()?
You can use the module Devel to print the value of the $form array which will contain the form information you are looking for.
Example:
dpm($form);
You will be able to easily see the content of the form and access it with php in array form.

Getting random path hierarchy for CCK fields inside form array

I have created a custom entity and i'm using CCK fields. Each bundle has it's own fields. For example:
function MYMODULE_install() {
// Check if our field is not already created.
if (!field_info_field('field_myField')) {
$field = array(
'field_name' => 'date_field',
'type' => 'list_text',
);
field_create_field($field);
}
//Enable is executed only once.
function bundle_callback_enable() {
// Create the instance on the bundle.
$instance = array(
'field_name' => 'date_field',
'entity_type' => 'payment_method',
'label' => 'Expiration Date',
'bundle' => 'card',
'required' => TRUE,
'settings' => array();
field_create_instance($instance);
}
My bundles are created from individual modules, so in each install file i am creating the respective fields.
Yesterday i tried to add validation callback functions in those fields and i saw something weird inside form array. Fields with type="text" had the path:
$form[field_name]['und'][0][value] //<! expectable
but fields with type='list_text' had only the path:
$form[field_name]['und'] //<! unexpectable
I couldn't find any solution and i've solved it with this:
function &get_cck_path_value( $field_name, &$form_path) {
$field = null
if ( isset( $form_path[$field_name][LANGUAGE_NONE] ) ) {
$field = &$form_path[$field_name][LANGUAGE_NONE]
}elseif(isset($form_path[$field_name][LANGUAGE_NONE][0])) {
$field = &$form_path[$field_name][LANGUAGE_NONE][0]['value'];
}
return $field;
}
I don't like this approach. Is too hucky. Can you tell me if that is a cck feature or bug?
I can't understand when it decides where to put the values( All the process is fulfilled through the "field_attach_form( ... )" )?
Have you faced any problem like this?
Thanks in advance.
Thandem.
I believe that you are seeing the abbreviated form field in validation because the field had no value entered into it and no default value was defined for it. There is no value, so no array is present to store the value.

Search for Custom Field with Custom Post Types

There are tutorials that explain how to limit a search to a specific category.
My question is, is there a way to configure wordpress' search to, within a custom post type, search for a custom field value.
So for example, if I search for "hello", the results would come up with posts that have a certain custom field equal to "hello". The certain post would also be a certain custom post type.
Any help is appreciated.
To filter search by custom post type use:
<?php query_posts($query_string . '&post_type=custom-post-type-name' ); ?>
before the loop.. then within the loop add a condition similar to this
<?php if ($meta_data[ 'meta-name' ] == 'hello') {
//do something
} ?>
I think here is what you are looking for:
key is the custom field.
value is the value you are looking for
and compare is the operator you want to use.
you can also use LIKE if you want.
// WP_Query arguments
$args = array (
'post_type' => 'vendors',
'post_status' => 'published',
'meta_query' => array(
array(
'key' => 'state',
'value' => 'Misissipi',
'compare' => '=',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();

Resources