Hi
Below is code i am using in widget but i can’t get option of select field. I see document guide select field type require associative array so get_search_course_cat function also return associative array term id as keys, term names as values but i am fail to get select field with taxonomy option. Please guide me
array( 'title' => array( 'type' => 'text', 'label' => __('Title', 'edupress'), 'default' => __('Search By Category', 'text-domain'), ), 'course_cat' => array( 'type' => 'select', 'label' => __( 'Choose a thing from a long list of things', 'edupress' ), 'multiple' => true, 'options' => $this->get_search_course_cat(), ), ), function get_search_course_cat() { return get_terms( 'course_category', 'orderby=name&fields=id=>name&hide_empty=0' ); }
Hi ahmed17,
I don’t think you’ll be able to use get_terms() directly as it returns invalid data. As per the docs, you’ll want your array to be structured like so:
Ignoring that issue, get_terms() by default will return an object. Please refer to the codex for more information on how to return an associative array
I am using get_terms function https://developer.wordpress.org/reference/functions/get_terms/ .
You can see $args argument list in above codex url there is fields argument ‘id=>name’ (returns an associative array with ids as keys, term names as values), i double check it’s return array like
Thanks
Ahmed
Hi Ahmed17,
Well, I feel silly – You’re right. I’ll ask the development team about this as local tests say there’s nothing wrong with your query.
One possibility is that because of the initialization order of WordPress, the get_terms function isn’t returning anything when the widget is being initialized.
One option would be to use the modify_form function to add in the options later. This function is only called right before the form is rendered, so there’s a much better chance that get_terms is able to return something.
Here’s an example of using modify_form:
https://github.com/siteorigin/so-widgets-bundle/blob/develop/widgets/features/features.php#L269
Greg,
Thank you so much it’s working.
Greg,
How Can i access widget field from outside widget ? Actually i am using media field to input image for widget background so i want to access image field outside widget. I know there is way we can apply style using style property of div but i am developing themeforest theme so it’s not allow to generate css using style property for validation purpose.
For example i am using below bgimage field in widget how can i access it from outside widget?
Thanks
Hi Ahmed
To answer your question, it is technically possible to access the widget data, outside the widget, but it’s very complicated. You’d essentially need to look at the code WordPress or Page Builder (if you’re using it) use to fetch the widget data from the database and recreate that. WordPress stores widget instances in a global variable and Page Builder stores it in post meta.
So while it’s technically possible, it’s a little complex, so finding a way to implement what you need using only the widget function would be ideal.
Greg,
Thank you so much. I really appreciate your help.
Thanks
Ahmed
It is sooo important to me – but I didn’t understand it.
Is there an possibility to add a custom dropdown to select a category? The build in post-loop-option misses some options i need urgently like the post-offset. But to be honest I have absolutly NO glue how to move on. An example to show how to add such a dropdown would be really really cool!
Thank you
Hi Roya
Unfortunately, we can’t help you with doing this as it’s beyond the scope of what we can help you with on the forums. With that said, you could hire a developer to implement this for you if it’s important to your website. We highly recommend using Codeable.
To clarify, you’re able to set an offset in the Post Loop widget via the additional field.
Hi Alex,
I like you guys, you’re responding so fast for a free plugin. Thank you so far!
I am pretty shure I could do it my own, I am just a little bit lost.
For the moment I’ll use the additional field (for me for testing purposes it’s enough, but I think it’s not comfortable for the enduser as they have no idea about hooks and loops).
All the best