31946

Get an array of category ids in wordpress?

Question:

$cats= get_categories(array( 'order' => 'ASC', 'orderby' => 'id', 'hierarchical' => 0, 'hide_empty' => 0, 'taxonomy' => 'edu_year' ));

I would like to produce a variable that contains an array of the IDs of the values retrieved from the get_categories function.

Answer1:

Based on the documentation it returns:

$category->term_id $category->name $category->slug $category->term_group $category->term_taxonomy_id $category->taxonomy $category->description $category->parent $category->count $category->cat_ID $category->category_count $category->category_description $category->cat_name $category->category_nicename $category->category_parent

So:

$output_categories = array(); $categories=get_categories($args); foreach($categories as $category) { $output_categories[$category->cat_ID] = $category->name; }

Then $output_categories is exactly what you need.

Answer2:

When you use <a href="http://codex.wordpress.org/Function_Reference/get_terms" rel="nofollow">get_terms</a>:

<?php $categories_ids = get_terms( array( 'category' ), // Taxonomies array( 'fields' => 'ids' ) // Fields ); ?>

Recommend

  • How to wrap php code inside a function? [closed]
  • Detect is a website address or email address on textblock
  • Invalid argument supplied for foreach() .. Wordpress
  • Displaying search term (Tag/Archive) in WordPress
  • How to get data from URL using Windows and C?
  • Save and retrieve an image file in LibGDX
  • Modify a Google App Engine entity id?
  • Sql indexes vs full table scan
  • Scrolling News Ticker Jquery - Issues
  • Using extern @class in order to add a category?
  • “A GKScore must specify a leaderboard.”
  • How can I see a list of all files that are different between two Hg repositories?
  • Receiver has no segue with identifier“***”
  • Updating Dojo provide
  • Multiple producers single consumer locking schema
  • Access Android Market through SSH tunnel
  • HTTP/2 streams vs HTTP/1.1 connections
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Use of this Javascript
  • C++ Partial template specialization - design simplification
  • ImageMagick, replace semi-transparent white with opaque white
  • Fill an image in a square container while keeping aspect ratio
  • Convert array of 8 bytes to signed long in C++
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • How to get next/previous record number?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • How to format a variable of double type
  • Python: how to group similar lists together in a list of lists?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • KeystoneJS: Relationships in Admin UI not updating
  • Understanding cpu registers
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?