Ask your WordPress questions! Pay money and get answers fast! (more info)

Multidimensional Array - Access Name&Slug WordPress

  • SOLVED

I have set up a plugin with multiselect checkboxes to allow end users to select multiple categories, which I can access through get_option ('jqs_select_categories'). This is stored in wp_options as an array such as : array(3) { [0]=> string(4) "Bath" [1]=> string(7) "Kitchen" [2]=> string(5) "Patio" }.

However, I need to access the name and slug of the selected categories. For example:

$categories = get_option ("jqs_select_categories");
foreach ($categories as $category) {
echo $category->name;}

This outputs nothing. How can I access the name and slug of the selected categories?


For reference, the categories are called as follows:

$jqs_categories = array();
$jqs_categories_args = array('hide_empty' => 1, 'builtin' => false);
$jqs_categories_obj = get_categories($jqs_categories_args);
foreach ($jqs_categories_obj as $jqs_cat) {
$jqs_categories[$jqs_cat->cat_ID] = $jqs_cat->cat_name;}
$categories_tmp = array_unshift($jqs_categories);

......

$FPFoptions = array (
array (
'name' => 'Categories',
'id' => $FPFshortname.'select_categories',
'type' => 'multiselect',
'options' => $jqs_categories,
),
);

......

foreach ($FPFoptions as $value) { if ($value['type'] == 'multiselect') { ?>

<div class="wrap">
<h3><?php echo $value['name']; ?></h3>
<?php $categories = get_option("jqs_select_categories"); ?>
<?php foreach ($value['options'] as $FPFoption) { ?>
<label for="<?php echo $FPFoption; ?>">
<input type="checkbox" name="my_name[]" id="<?php echo $FPFoption; ?>"
value="<?php echo $FPFoption; ?>"
<?php if(in_array($FPFoption, $categories)) { echo ' checked="checked"'; } ?> /> <?php echo $FPFoption; ?>
</label>
<?php } ?>
</div><!-- .wrap -->

} }

Answers (4)

2014-05-16

Kyle answers:

Did you try with get_term_by ?

Depending whatever you are strong, looks like name, you can grab your term info inside the loop

$categories = get_option ("jqs_select_categories");
foreach ($categories as $category) {
$term = get_term_by('name',$category,'category');
}

Then that term object will return all the info


Louise comments:

Hi there - I think this is really close. It works to output names as :

$categories = get_option('jqs_select_categories');
foreach ($categories as $category) {
$term = get_term_by('name',$category,'category');

echo $term->name; }

But when I switch over to slug, it's not picking up the multi-word categories i.e. "Bath Products."

$categories = get_option('jqs_select_categories');
foreach ($categories as $category) {
$term = get_term_by('slug',$category,'category');

echo $term->slug; }


Any idea?


Kyle comments:

Keep it as $term = get_term_by('name',$category,'category');

Then that $term var has all the data. So you can do $term->slug will work

You can do all these from there:
$term->term_id
$term->name
$term->slug
$term->term_group
$term->term_taxonomy_id
$term->taxonomy
$term->description
$term->parent
count


Kyle comments:

Last one was supposed to be $term->count

:)


Louise comments:

Ok, now if I need the $term var further down in my code.... will it still work?

First...
$categories = get_option('jqs_select_categories');

$types = $categories

foreach ($types as $type) {

$term = get_term_by('name', $type, "category");

$type_list .= '<li><a href="#" class="'. $term->slug .'">' . $term->name . '</a></li>';

echo $type_list; }

Further down within the same function....

$types = get_the_category( get_the_ID(), $categories); ?>

<li id="item" data-id="id-<?php echo $count; ?>" data-type="<?php foreach ($types as $type) { echo $term->slug. ' '; }?>">


Thanks!





Kyle comments:

Yes, it will work inside the entire foreach brackets


Louise comments:

But, in this case $types is defined as two separate things....

So in the bottom part of the code, would I need to redefine $term-> slug? It's outside the original foreach brackets.


Kyle comments:

Yes, you'll have to do the same thing.

If you are referring to the part at the bottom of your code it would be

foreach ($value['options'] as $FPFoption) {

$term_2 = get_term_by('name', $FPFoption, 'category' );

}


Louise comments:

Sorry, to be clear I mean this code.... how would it work in Part 2? Part 2 is outside the foreach brackets in Part 1.

PART 1
$categories = get_option('jqs_select_categories');

$types = $categories

foreach ($types as $type) {

$term = get_term_by('name', $type, "category");

$type_list .= '<li><a href="#" class="'. $term->slug .'">' . $term->name . '</a></li>';

echo $type_list; }

PART 2

$types = get_the_category( get_the_ID(), $categories); ?>

<li id="item" data-id="id-<?php echo $count; ?>" data-type="<?php foreach ($types as $type) { echo $term->slug. ' '; }?>">


Kyle comments:

Just so I'm clear, for part 2 are you trying to make a list of categories for the current post?


Kyle comments:

If so, the object returned by [[LINK href="http://codex.wordpress.org/Function_Reference/get_the_category"]]get_the_category[[/LINK]] is a little different.

You will have to add in [0], so:

$types[0]->slug


Kyle comments:

Your part two probably should be more like this

<?php $types = get_the_category( get_the_ID() ); ?>

<?php foreach ($types as $type) { ?>

<li id="item" data-id="id-<?php echo $types[0]->count; ?>" data-type="<?php echo $types[0]->slug. ' '; ?>">

<?php } ?>


Kyle comments:

Correction:

<li id="item" data-id="id-<?php echo $type[0]->count; ?>" data-type="<?php echo $type[0]->slug. ' '; ?>">


Louise comments:

Part 1 lists out the selected categories (the filter). Part 2 is connecting posts inside the loop to the filter in Part 1 based on the data-id and the data-type. So my filter now correctly displays the selected categories, but it does not connect to the posts inside the loop. I.e. when you click "Kitchen", all posts with the "kitchen" category should display.

Am I going to run to the same issue trying to call 'get_option ("jqs_select_categories")' within get_the_category? And i guess I need to redefine $term->slug as well?

$categories = get_option ("jqs_select_categories")
$types = get_the_category( get_the_ID(), $categories); ?>

<li id="item" data-id="id-<?php echo $count; ?>" data-type="<?php foreach ($types as $type) { echo $term->slug. ' '; }?>">


Louise comments:

Thanks, what you posted doesn't tie back to my option - get_option ('jqs_select_categories')?


Kyle comments:

Not sure I know 100% what you mean.

You'll need to pass your selected filter var to a post query $query = new WP_Query( array( 'cat' => $categories );

Then you use that for your loop, and foreach post you can display its categories. Is that what you are trying to do?


Louise comments:

I'm using a JQuery Plugin to filter posts based on categories.

If I list out ALL categories with get_categories(); - I can successfully filter posts based on the category with the code immediately below. When I click on an individual category in the filter in Part 1 of the code, it will display only the posts associated with that filtered category.

$categories = get_categories();

query_posts(array( 'post_type' => $FPF_posts, 'posts_per_page' => $FPF_post_page_number, 'paged' => $paged) );

if( have_posts() ) : while (have_posts()) : the_post();

$types = get_the_category( get_the_ID(), $categories);

?>

<li id="item" data-id="id-<?php echo $count; ?>" data-type="<?php foreach ($types as $type) { echo $type->slug. ' '; }?>">


I am trying to modify it to account for the multiselect option to select categories within the plugin - get_option ('jqs_selected_categories'). So changed $categories to account for the option....

$categories = get_option("jqs_selected_categories");

query_posts(array( 'post_type' => $FPF_posts, 'posts_per_page' => $FPF_post_page_number, 'paged' => $paged) );

if( have_posts() ) : while (have_posts()) : the_post();

$types = get_the_category( get_the_ID(), $categories);

?>

<li id="item" data-id="id-<?php echo $count; ?>" data-type="<?php foreach ($types as $type) { echo $type->slug. ' '; }?>">

I am struggling with the $type-> slug in this part of the code. Also, not sure if $categories = get_option("jqs_selected_categories") works inside get_the_category as an appropriate parameter.


Kyle comments:

If you are trying to filter the posts, you would need to drop your category option into the query

query_posts(array( 'post_type' => $FPF_posts, 'posts_per_page' => $FPF_post_page_number, 'paged' => $paged, 'cat' => $categories) );

Also need to make sure the $categories var is properly formatted.

get_the_category only accepts 1 parameter (post id) so not sure what you are trying to do with that part. That really would just be for displaying the posts categories in the loop, not for filtering - unless you did some kind of boolean check in the loop for the category

e.g. if( in_array($categories ,get_the_category( get_the_id() ) ){
//do stuff
}

in_array() would want $categories to be a single value there. If they are both arrays than you would use array_intersect() (note that returns an array of match values, it is no boolean)


Louise comments:

Hi again, I hear what you are saying. It's all specific to the jquery plugin, I guess and I'm probably causing more confusion.

To filter with taxonomies, I'm using - $types = get_the_terms( get_the_ID(), $taxonomies ); So I thought the appropriate equivalent was get_the_category. However, as categories are a multiselect checkbox option in my plugin, I'm stuck with how to make use of "get_option ("jqs_select_categories") instead of get_categories();. For example, $term->slug would still need to be defined in the second part of the code for it to work. Similarly, the output of get_option doesn't seem like the right parameter for get_the_category.


Kyle comments:

You can still use get_the_terms just set the taxonomy to category


Louise comments:

Oh, okay, I didn't realize that. But will it accept the option as a parameter? And then will it flow though to $term->slug? Or am I still going to run into my initial issue?

$categories = get_option("jqs_selected_categories");
$types = get_the_terms( get_the_ID(), $categories);

?>

<li id="item" data-id="id-<?php echo $count; ?>" data-type="<?php foreach ($types as $type) { echo $type->slug. ' '; }?>">

Thanks!


Kyle comments:

Provided the array returned by the get_option ("jqs_select_categories"); is okay it will work and yes it will return the slug like that


Louise comments:

I ended up getting it to work with get_the_category. Thanks for all of your help, much appreciated!

2014-05-16

Sébastien | French WordpressDesigner answers:

instead if your code :
$categories = get_option ("jqs_select_categories");
foreach ($categories as $category) {
echo $category->name;}


use this code :

foreach ($categories as $k=>$v) {
$slh_category_name = $v;
echo $slh_category_name;
}


tell me if it's ok


Louise comments:

Hi there - this is okay, but what about the slug?


Sébastien | French WordpressDesigner comments:

foreach ($categories as $k=>$v) {

//$categories = get_term_by('name', $v, "your_taxonomy_name");
//As you are using standard taxonomy, "category", you only have to set:
$categories = get_term_by('name', $v, 'category');

//print_r($categories);

foreach ($categories as $slh_cat) {
echo "term_id : " . $slh_cat->term_id . "<BR>";
echo "name : " . $slh_cat->name . "<BR>";
echo "slug : " . $slh_cat->slug . "<BR>";
echo "term group : " . $slh_cat->term_group . "<BR>";
echo "term taxo id : " . $slh_cat->term_taxonomy_id . "<BR>";
echo "taxo : " . $slh_cat->taxonomy . "<BR>";
echo "description : " . $slh_cat->description . "<BR>";
echo "parent : " . $slh_cat->parent . "<BR>";
echo "count : " . $slh_cat->count . "<BR>";
}

}


Sébastien | French WordpressDesigner comments:

as you can see, the code below displayed several informations related to your category (name, slug, id etc...)


Sébastien | French WordpressDesigner comments:

have you try ?

2014-05-16

Hariprasad Vijayan answers:

Hi,

<blockquote>
$categories = get_option ("jqs_select_categories");
</blockquote>

Are you getting any value in $categories?


Louise comments:

Yes, if I were to do

$categories = get_option ("jqs_select_categories");
foreach ($categories as $category) {
echo $category}

I can successfully call the selected categories.

However, I need the slug & name values to integrate into my existing code. Thanks.


Hariprasad Vijayan comments:

Could you show the result of following code

$categories = get_option ("jqs_select_categories");
print_r($categories);


Louise comments:

Sure - Array ( [0] => Bath [1] => Kitchen [2] => Patio )

Based on selecting the Bath, Kitchen and Patio categories.

2014-05-16

Francisco Javier Carazo Gil answers:

Louise,

You have to do the next:

$categories = get_option ("jqs_select_categories");


foreach ($categories as $id_category) {

$category = get_term($id_category, "your_taxonomy_name");
echo $category->name;

}


Francisco Javier Carazo Gil comments:

As you are using standard taxonomy, "category", you only have to set:


$categories = get_option ("jqs_select_categories");

foreach ($categories as $id_category) {

$category = get_term($id_category, "category");

echo $category->name;

}


Francisco Javier Carazo Gil comments:

Term object has more attributes:
<blockquote>term_id
name
slug
term_group
term_taxonomy_id
taxonomy
description
parent
count</blockquote>


Louise comments:

Hello again - This code is closest to what I need, however, this isn't outputting anything? Am I missing something?


$categories = get_option ("jqs_select_categories");
foreach ($categories as $id_category) {
$category = get_term($id_category, "category");
echo $category->name;
}


Francisco Javier Carazo Gil comments:

Could you send me the var_dumps?

$categories = get_option ("jqs_select_categories");
var_dump($categories);
foreach ($categories as $id_category) {
$category = get_term($id_category, "category");
var_dump($category);
echo $category->name;
}


Louise comments:

Sure, here's what came out -

array(3) { [0]=> string(4) "Bath" [1]=> string(7) "Kitchen" [2]=> string(5) "Patio" } NULL NULL NULL


Francisco Javier Carazo Gil comments:

Perfect :)

$categories = get_option ("jqs_select_categories");

foreach ($categories as $id_category) {
$category = get_term_by("name", $id_category ,"category");
echo $category->name;
}

Try now.