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.

If trying to filter out some return results from a custom post type that has a custom field appended to it for location. I'm not too sure where I need to apply the case statement in my query. Or if I need to add it into the query when it pulls the info from the post type.

I currently have this:

<?php
$mypost = array('post_type' => 'staff', 'posts_per_page' => 20 );
$loop = new WP_Query( $mypost );
 ?>
<ul>

<?php while ( $loop->have_posts() ) : $loop->the_post();?>

<?php $items = get_post_meta( $post->ID, 'elements', true ); 
foreach( $items as $i){
if($i['location'] = 'local') {
echo'<li>
    <a href=" '. get_permalink() .' ">
    <img src=" '. wp_get_attachment_url( $i['small-image'] ) .'" />
    <h4>' . $i['staff-name'] . '</h4>
    <p>' . $i['short-title'] . '</p>
    ' . $i['3-areas-of-focus'] . '
    </li></a>

    ';
    }
}
?>  

<?php endwhile; wp_reset_query(); ?>
<div class="clearfix"></div>

</ul>

I have the custom field for the location as a checkbox, so inside the custom post type it shows:

local, out of town

The thing is that some staff members can have a location local and out of town, but some are just local.

So, I'm not sure if I need to have do a trim explode before or if there is some other way that I need to have it look up in the query or what.

share|improve this question
    
What case statement? This looks like a pure PHP question on the face of it. Where is the WordPress component? –  s_ha_dum Nov 15 '13 at 0:48
add comment

2 Answers

up vote 0 down vote accepted

Ok, I got it work.

Since the checkbox for the custom fields stores entires as a comma seperated value, I needed to explode the delimiter and then perfom another loop to parse the information.

Then I created two separate arguments to split the data and call it back within the echo.

Here's my final output of the code:

<?php
$mypost = array('post_type' => 'staff', 'posts_per_page' => 20 );
$loop = new WP_Query( $mypost );  
?>
<ul>

 <?php while ( $loop->have_posts() ) : $loop->the_post();?>

 <?php $items = get_post_meta( $post->ID, 'elements', true ); 

 foreach($items as $i) {

$loco = explode("," , $i['locations']);

    foreach ($loco as $l) {

        if ($l != 'local') {

            echo'<li>
                    <a href=" '. get_permalink() .' ">
                <img src=" '. wp_get_attachment_url( $i['small-image'] ) .'" />
                <h4>' . $i['staff-name'] . '</h4>
                <p>' . $i['short-title'] . '</p>
                ' . $i['3-areas-of-focus'] . '
                </li></a>
                ';
        }

    }
}
?>  

<?php endwhile; wp_reset_query(); ?>
<div class="clearfix"></div>

</ul>

I know this might not be the most elegant solution, but it's working within my custom post types.

share|improve this answer
add comment

Or if you already know what type of combinations can your custom post meta value have, you may want to do a comparison like:

if($i['location'] == 'local' || $i['location'] == 'out of town' || $i['location'] == 'local, out of town') {
// your code here
}

As a side note, to do a equality comparison in if statement, you need == instead of a single = equal sign. Single = sign will actually assign the variable the value which you want to compare and it will always return true.

share|improve this answer
add comment

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.