Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have php set up in a template file to check a form and see if 1 or more of 5 categories is selected. Based on which one is selected, I want the custom query I'm using to change in that the array for "category_name" changes depending on what is selected.

I have gotten as far as defining the variables from the form but I can't figure out how to get the multiple variables to put themselves into an array. Here is an example of what I'm trying to do:

        if (isset($_POST['showvideo']) && ($_POST['showvideo'] == 'true')) :
        $video='user-video,';
    endif;
    if (isset($_POST['showimage']) && ($_POST['showimage'] == 'true')) :
        $image='user-image,';
    endif;
    if (isset($_POST['showaudio']) && ($_POST['showaudio'] == 'true')) :
        $audio='user-audio,';
    endif;
    if (isset($_POST['showwriting']) && ($_POST['showwriting'] == 'true')) :
        $writing='user-writing,';
    endif;
    if (isset($_POST['showevent']) && ($_POST['showevent'] == 'true')) :
        $event='user-event';
    endif;

And then, I don't know how to get each of those values to put themselves into a correct array to use inside my custom query as the 'category_name' array.

This is obviously wrong:

$upostcat= array ($video . $image . $audio . $writing . $event);

Can someone help me with how to do this?

Updated WORKING code based on answer below (thanks!):

    if (isset($_POST['showvideo']) && ($_POST['showvideo'] == 'true')) {
    $cats[]='user-video';
} elseif (isset($_POST['showimage']) && ($_POST['showimage'] == 'true')) {
    $cats[]='user-image';
} elseif (isset($_POST['showaudio']) && ($_POST['showaudio'] == 'true')) {
    $cats[]='user-audio';
} elseif (isset($_POST['showwriting']) && ($_POST['showwriting'] == 'true')) {
    $cats[]='user-writing';
} elseif (isset($_POST['showevent']) && ($_POST['showevent'] == 'true')) {
    $cats[]='user-event';
};

if (!empty($cats) {
  $upostcat = implode(',',$cats);
};
share|improve this question
    
The commas that appear after each variable value are only there because I'm not sure how to do the separators in the array. I'm sure that's totally wrong too... –  Eckstein Dec 9 '12 at 19:58

1 Answer 1

up vote 1 down vote accepted

To make an array, separate your values with commas rather than concatenate them with periods.

$upostcat= array (
  $video,
  $image,
  $audio,
  $writing,
  $event
);

That part is pure PHP. However, I notice you want to use the category_name "array". If you are talking about WP_Query, category_name does not accept an array-- really this is the only WordPress part of your question, by the way. What you actually want is a string with values separated by commas, like this from the Codex:

$query = new WP_Query( 'category_name=staff,news' );

For that you do need to concatenate. What you've done should mostly work except that you have a high probability of having a trailing comma. Imagine that one or more of the first four are set and the last one isn't. There is an easier way.

if (isset($_POST['showvideo']) && ($_POST['showvideo'] == 'true')) {
    $cats[]='user-video';
} 
if (isset($_POST['showimage']) && ($_POST['showimage'] == 'true')) {
    $cats[]='user-image';
} 
if (isset($_POST['showaudio']) && ($_POST['showaudio'] == 'true')) {
    $cats[]='user-audio';
} 
if (isset($_POST['showwriting']) && ($_POST['showwriting'] == 'true')) {
    $cats[]='user-writing';
} 
if (isset($_POST['showevent']) && ($_POST['showevent'] == 'true')) {
    $cats[]='user-event';
};
if (!empty($cats)) {
  $upostcat = implode(',',$cats);
}
share|improve this answer
    
Thanks for you response! Makes sense about the array, that was dumb of me. I have tried this code but it is throwing an error. I updated the code on the original question. Does it look right? –  Eckstein Dec 9 '12 at 20:39
    
Its a typo :( One of the lines near the end was missing a ")". –  s_ha_dum Dec 9 '12 at 20:53
    
Ah, I see it now. So the error is gone, but the output of $upostcat seems to only be the word 'array' and not the array itself. I realize at this point that this isn't Wordpress related, so feel free to answer if you have any other advice, otherwise I can go post on a more appropriate board. Thanks again for your help though. –  Eckstein Dec 9 '12 at 21:13
    
You've recreated $upostcats as an array at the bottom of your code. Don't do that. You need a string, which is what my code gives you. –  s_ha_dum Dec 9 '12 at 21:41
    
Got it! Thank you, this was very helpful. –  Eckstein Dec 9 '12 at 21:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.