0

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);
};
1
  • 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
    Commented Dec 9, 2012 at 19:58

1 Answer 1

1

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);
}
5
  • 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
    Commented Dec 9, 2012 at 20:39
  • Its a typo :( One of the lines near the end was missing a ")".
    – s_ha_dum
    Commented Dec 9, 2012 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
    Commented Dec 9, 2012 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
    Commented Dec 9, 2012 at 21:41
  • I had to make one more change to get what I wanted, which was to make sure that multiple categories could be displayed at once. To achieve this, I just changed the elseifs to ifs and it works perfectly. Just FYI for anyone looking to do this.
    – Eckstein
    Commented Dec 9, 2012 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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