Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Get custom meta value from select menu in post edit screen WordPress

  • SOLVED

Hi,

I hope the topic title isn't all that confusing for all!

I have a custom meta box in my post edit screen where the user can choose an value from a select menu.

The values in the select menu need to be the categories of the blog itself.

I am using the Meta Box Class from WPAlchemy [[LINK href="http://www.farinspace.com/wpalchemy-metabox/"]][[/LINK]]

Here's an example of the select menu code included with the WPAlcheme Meta Box class,


<?php $mb->the_field('select'); ?>
<select name="<?php $mb->the_name(); ?>">
<option value="">Select...</option>
<option value="a"<?php $mb->the_select_state('a'); ?>>a</option>
<option value="b"<?php $mb->the_select_state('b'); ?>>b</option>
<option value="c"<?php $mb->the_select_state('c'); ?>>c</option>
</select>


And here's an example of what I tried,


<?php $mb->the_field('select'); ?>
<select name="<?php $mb->the_name(); ?>">
<option value="">Select...</option>
<option value="<?php wp_dropdown_categories(); ?>"<?php $mb->the_select_state('a'); ?>></option>

</select>


And in my post template I would use,


<?php $_custom_meta = get_post_meta($post->ID,'_custom_meta',TRUE); echo $_custom_meta['select'];?></span>



While I can the select box to appear in my custom meta box in the post edit screen, when I update the post the value resets itself so obviously it's wrong, very wrong.

So I am wondering how I can achieve the above through this or any other method?

I hope this all makes sense guys.

Thank you very much!

Answers (1)

2011-06-02

Christianto answers:

Hi,

I think you have to match between value of your dropdown category with <?php $mb->the_select_state('a'); ?> that would print selected="selected" attribute on option.

and more over you use wp_dropdown_categories(); for the value, this function will return html dropdown menu not any categories ID.

You have to take all id with get_all_category_ids() result in array and create custom loop that would print those category as <option> inside <select> tag and check if this value is exists as value saved on database with <?php $mb->the_select_state('ID'); ?> where ID is the id of category currently loop.

Hope this help.



Wordpressing comments:

Hi Christiano,

Thanks for your reply.

Do you have any examples of the code you are mentioning or where I could look to learn how to write such an array?

Thank you again


Christianto comments:

Example code:

<select name="<?php $mb->the_name(); ?>">
<option value="">Select...</option>

<?php

$IDs = get_all_category_ids();

foreach($IDs as $ID){ ?>

<option value="<?php echo $ID ?>"<?php $mb->the_select_state($IDs); ?>></option>

<?php } ?>

</select>


Christianto comments:

Sorry, I'm not familiar with the class.
And there is typos of 'IDs' inside the loop.
I think you have multiple select category you have to add the name to check

<select name="<?php $mb->the_name(); ?>">
<option value="">Select...</option>

<?php

$IDs = get_all_category_ids();
$NAME = $mb->the_name();
foreach($IDs as $ID){ ?>

<option value="<?php echo $ID ?>"<?php $mb->the_select_state($NAME, $ID); ?>></option>

<?php } ?>

</select>


Wordpressing comments:

Hi Christiano,

Thanks, this seems to be getting closer.

Using get_all_category_ids() the select menu returns the ID number of each category and I am able to print the category ID in my post template but do you have an idea how I may convert into a name?

Thank you


Christianto comments:

You mean category name?
use get_cat_name() function.

<select name="<?php $mb->the_name(); ?>">
<option value="">Select...</option>

<?php

$IDs = get_all_category_ids();
$NAME = $mb->the_name();
foreach($IDs as $ID){
$cat_name = get_cat_name($ID);
?>
<option value="<?php echo $cat_name; ?>"<?php $mb->the_select_state($NAME, $cat_name); ?>></option>

<?php } ?>

</select>


Wordpressing comments:

Thanks Christiano that works...

Can I ask one more thing, is it possible to print this value as a link to the category?

In my single.php template file I am using,

<?php $_custom_meta = get_post_meta($post->ID,'_custom_meta',TRUE); echo $_custom_meta['single'];?>

To retrieve the value of the select menu code as listed above which works but it'd be preferred if I can have it also take the permalink to the category too...

I'll throw in another $5 if you want?


Christianto comments:

Ok,

First we have to get the category id with category name.
And then get the category link with get_category_link() function.
Assuming the category name store in $_custom_meta['single']

<?php

$_custom_meta = get_post_meta($post->ID,'_custom_meta',TRUE);

// retrieve category ID
$catID = get_cat_ID($_custom_meta['single']);

// retrieve category link and echo it
echo get_category_link($catID);

?>


Christianto comments:

With <a> tag

<?php
$_custom_meta = get_post_meta($post->ID,'_custom_meta',TRUE);

// retrieve category ID
$catID = get_cat_ID($_custom_meta['single']);

// retrieve category link and echo it
$catLink = get_category_link($catID);
?>

<a href="<?php echo $catLink ?>" >Link to <?php echo $_custom_meta['single'] ?></a>


Christianto comments:

Any result Wordpressing?


Wordpressing comments:

Hi Christiano,

I thought I had it all working in my (front end) template file last evening but it appears not.

So far with the help of your code for the post edit screen meta box I am able to generate values in a select menu based upon my sites categories.

As part of the meta box class (WPAlchemy) I can also add or remove multiple instances of this select field in case I want to display multiple values in my template file.

Using the following code I am able to generate the values in my template file,

<?php while($custom_metabox->have_fields('s_group'))
{
echo '<li><a href="'.get_category_link($catID).'">'.$custom_metabox->get_the_value('s_single').'</a> </li>';
}
;?>



But the links that are generated for the value point to the post name instead of the category that matches the value "s_single".

This is probably because I have not declared the $catID variable in this "while" statement as I'm not sure where to put "$catID = get_cat_ID($_custom_meta['s_single']);"

PS. Using your block of code directly above produces the same result - except for only one value as it's not checking for multiple instances. Never the less it only points to the post permalink instead of the category. Like I said I thought I had this working last night after your assistance but now I am really stumped!

Thank you for your help thus far,
Word


Wordpressing comments:

This problem has been solved and the code used to achieve that was,


<?php

while($custom_metabox->have_fields('s_group'))

{

$catID = get_cat_ID($custom_metabox->get_the_value('s_single'));
echo '<li><a href="'.get_category_link($catID).'">'.$custom_metabox->get_the_value('s_single').'</a> </li>';

}
?>


For anyone who may be a little confused by the use of the above code I am using the Meta Box class written by Farinspace.com called WPAlchemy which is a great little helper class for creating your own custom meta boxes.

Thanks to Christiano for his help.