Rebuilding arrays

I’m working on a tool that fetches stuff from the WordPress database and handles it in different ways. One of the things I’ve done is build a simple array called $category, which contains the contents of wp_terms where term_id = <what the user picked>.

Unfortunately, it doesn’t contain everything I want, so I’ve also populated $category->taxonomy with the relevant details from wp_term_taxonomy where term_id = <what the user picked>. It’s a one-to-one relationship, so no problems there.

Read More

However, I’ve also got a bunch of stuff I need in wp_termsmeta, where there’s a one-to-many relationship of multiple lines with the same term_id. So what I get is something like this:

{
  "term_id" : 9813,
  "name" : "Who's Who",
  "slug" : "ww",
  "term_group" : 0,
  "taxonomy" : 
  {
    "term_taxonomy_id" : 9832,
    "term_id" : 9813,
    "taxonomy" : "category",
    "description" : "Who's Who is the CSICON Network's very own show about Doctor Who!",
    "parent" : 0,
    "count" : 58
  },
  "meta" : [
    {
      "meta_id" : 8490,
      "terms_id" : 9813,
      "meta_key" : "hosts",
      "meta_value" : "1, 2"
    },
    {
      "meta_id" : 8492,
      "terms_id" : 9813,
      "meta_key" : "thumbnail",
      "meta_value" : "http://csicon.fm/wp-content/uploads/2015/12/ww_4-248x248.jpg"
    }
  ]
}

We’ve got term_id, name, slug, and term_group from wp_terms in $category, then a bunch of stuff in $category->taxonomy and then a bunch of things under $category->meta.

And now for the problem.

If I want to access, say, the thumbnail URL, I’m going to have to traverse down to $category->meta[1]->meta_value, but I can’t always know that it’ll be [1]. For other categories, it might be [0] or [2]. All I know for sure is that it’s the [n] that has meta_key = thumbnail.

Ideally, I would want a good way to iterate through meta and simply save the values for each meta_key as a “real” key under $category, so $category->thumbnail would return the meta_value currently stored there … But I’ll settle with a reliable way of always finding the right meta_value for a specific meta_key.

Any thoughts? 🙂

SOLVED

Thanks! Instead of $category['meta'] = Category::find($term_id)->catMeta;, I’m now running the following code, and it does exactly what I wanted.

$metaTemp = Category::find($term_id)->catMeta;
$metaTable = json_decode(json_encode($metaTemp), FALSE);
foreach($metaTable as $key => $value) {
    $category[$value->meta_key] = $value->meta_value;
}

Related posts

2 comments

  1. I would suggest converting meta from an array to an object where properties of that object would be meta_keys. So you get this structure:

    {
      "term_id" : 9813,
      "meta" : {
        "hosts" : {
          "meta_id" : 8490,
          "terms_id" : 9813,
          "meta_key" : "hosts",
          "meta_value" : "1, 2"
        },
        "thumbnail" : {
          "meta_id" : 8492,
          "terms_id" : 9813,
          "meta_key" : "thumbnail",
          "meta_value" : "http://csicon.fm/wp-content/uploads/2015/12/ww_4-248x248.jpg"
        }
      }
    }
    

    So that way search for the right meta would have a constant cost in time rather than linear.

    your clients would search for it like so: $category->meta->hosts

  2. Thanks! Instead of $category['meta'] = Category::find($term_id)->catMeta;, I’m now running the following code, and it does exactly what I wanted.

    $metaTemp = Category::find($term_id)->catMeta;
    $metaTable = json_decode(json_encode($metaTemp), FALSE);
    foreach($metaTable as $key => $value) {
        $category[$value->meta_key] = $value->meta_value;
    }
    

Comments are closed.