add DB query to $data array, then iterate over it in other file WordPress

I have a function.php file in my plugin I am making and I want a function that gets all data from a table in the DB then stores it in a $data array. My problem is when I call the function outside the functions file the array is null, if I dump the data array out inside the function and then call the function the data is present so I know the query is successful. Here is my code:


    function getCategories() {

    $data = array();

    global $wpdb;
    $data = $wpdb->get_results("SELECT * FROM `metal_work_categories` WHERE 1", ARRAY_N);





I also tried initializing the data array in the index.php file and passing it to the function, just a wild stab in the dark really, to no avail:

$data = array();


That's how variable scope works. You need to return the data you want from the function, and assign it to a variable.

function getCategories() {
   global $wpdb;
   return $wpdb->get_results("SELECT * FROM `metal_work_categories` WHERE 1", ARRAY_N);

$data = getCategories();

PS: Next up, stop using global!

Tags: PHP / Wordpress

Similar questions

Iterate through array in WordPress using shortcodes
I am pretty new with WP, though I know how to write basic plugin functionality and create shortcodes. I want to have my plugin return an array (a list of the user's favorites), but have the HTML defined in the post, not in the shortcode implementation. So I want to do something along these lines: I know how to embed the HTML in the shortcode defini...
Wordpress: Iterate through multiple api fetches within for each loop. Only returns response for first array item
This issue is happening on a wordpress theme. In short, I previously was iterating through an array of id's and doing a wp_remote_get to an external api for each id. It worked but created page load issues, so refactoring to use ajax and now the wp_remote_get only works on first array item. Here's an example of how the API url is structured: http://...
iterate through unserialized wordpress options array in php
I am trying to ultimately grab the wordpress plugin slug and new version values from this object array. I've done a lot of research and tried many examples but none really show this array format. I am confused by the index structure. Help is appreciated. Here is a snippet of the array structure from the unserialized wordpress data. I try this but g...
If variation product then execute condition else if simple then execute other condition
I am using this function in price.php in my theme. This works well if the products are variable products, but fails when the product is simple. It throws a fatal error if the product is simple: I think this needs to be corrected:
How can i iterate through this shortcode array?
This is how $attr is created: This is the print_r of $attr in this post ( that has 3 graphs if it had more or less the array dimension would change): Then i need to pass $attr['footer_caption'] to a JS file where the highcharts plugin settings are being defined like this: And this is the setting in the js file where i pass the $attr['footer_caption...
how to properly iterate through array of post objects retrieved through Wp Rest Api in react js
hello :) i am working on wp rest api and react js and i successfully retrieved data from wp rest api and displayed it, but its not the proper way to display data in react. console is showing the error message of ' Each child in an array or iterator should have a unique "key" prop ' . I read the document in react regarding the issue but didn't under...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.