How to filter New WP Query by Custom Field Value?

I am creating new pages for each of my categories in wordpress. The post editor has a custom field that allows the selection of a sector type, this gets applied to the post on update. The custom field key is: sector, for custom field meta value options lets use SectorA, SectorB and SectorC. I am using a custom post type called projects.

I followed the advice at this link http://weblogtoolscollection.com/archives/2008/04/13/how-to-only-retrieve-posts-with-custom-fields/

Read More

How can I change the query line in the code below so that it filters the loop by a Sector name, lets use SectorA. I’ll then reuse the code on each template page changing the value to SectorB and SectorC on the other pages.

I think this needs changing somehow:

$customPosts->query('showposts=5&sector=sectorA&post_type=projects' );

Currently it echos the sector value and description value successfully but is showing all the posts. So my attempt to limit it to sectorA using sector=sectorA doesn’t seem to work?

This code is in functions.php:

function get_custom_field_posts_join($join) {
global $wpdb, $customFields;
return $join . "  JOIN $wpdb->postmeta postmeta ON (postmeta.post_id = $wpdb->posts.ID and postmeta.meta_key in ($customFields)) ";
}
function get_custom_field_posts_group($group) {
global $wpdb;
$group .= " $wpdb->posts.ID ";
return $group;
}

And this code is on the Template Page:

<?php /* Begin Custom Field Posts */ ?>
<h2>Custom Posts</h2>
<ul>
<?php 
global $customFields;
$customFields = "'sector', 'description'";
$customPosts = new WP_Query();
add_filter('posts_join', 'get_custom_field_posts_join');
add_filter('posts_groupby', 'get_custom_field_posts_group');
$customPosts->query('showposts=5&sector=sectorA&post_type=projects' );//Uses same parameters as query_posts
remove_filter('posts_join', 'get_custom_field_posts_join');
remove_filter('posts_groupby', 'get_custom_field_posts_group');

while ($customPosts->have_posts()) : $customPosts->the_post(); 
$sector = get_post_custom_values("sector"); 
$description=  get_post_custom_values("description");?>
<li><?php echo $sector[0]; ?></li>
<li><?php echo $description[0]; ?></li><br />
<?php endwhile; ?>
</ul>
<?php /* End Custom Field Posts */ ?>

Thanks for your help

Related posts

Leave a Reply

2 comments

  1. May be this what you want

    function get_custom_field_posts_join($join) {
    global $wpdb, $customSector;
    return $join . "  JOIN $wpdb->postmeta postmeta ON (postmeta.post_id = $wpdb->posts.ID and postmeta.meta_key = 'sector' and postmeta.value = '$customSector') ";
    }
    

    and modification of page

    $customSector='sectorA';//<--- insert this
    add_filter('posts_join', 'get_custom_field_posts_join');
    
  2. Try using this code.

    <?php
    $sector = get_post_meta($post->ID, "sector", false);
    if ($sector[0]=="") { ?>
    
    <!-- If there are no custom fields, show nothing -->
    
    <?php } else { ?>
    
    <div class="sector">
        <h3>Title</h3>
    
        <?php foreach($sector as $sector) {
        echo '<blockquote><p>'.$sector.'</p></blockquote>';
        } ?>
    
    </div>
    
    <?php } ?>