Only pull listings with the status of Available

Hi All,

I’m taking my first steps into php though WordPress, and although i seem to be about 90% of the way there in terms of where i would like the site to be, i just have a couple of final issues that i just can’t work out. To help me along the way i have used a few templates, which I’ve been able to customize to suit my needs.

I’m not sure how much detail i need to provided, but below is the php from the relevant widget page. What I’m after is to only show certain listings so for example i have it set up so you can specify if the car is sold or not. For this widget i only want to show the available cars. As i understand i need to include a statement within the ‘$args’ which specifies to only include listings with the status of available - but i have no idea as to how this would be done.

Hopefully this has made some sense and it’s possible to either point me in the right direction or show how the code would look. Also if i have missed any aspect this would need to be provided please let me know and i will upload.

Many thanks,

Keith

<?php


class ct_Listings extends WP_Widget {

   function ct_Listings() {
	   $widget_ops = array('description' => 'Display your latest listings.' );
	   parent::WP_Widget(false, __('Listings', 'contempo'),$widget_ops);
   }
						
   function widget($args, $instance) {
	extract( $args );
	$title = $instance['title'];
	$number = $instance['number'];
	$taxonomy = $instance['taxonomy'];
	$viewalltext = $instance['viewalltext'];
	$viewalllink = $instance['viewalllink'];
	?>
		<?php echo $before_widget; ?>
		<?php if ($title) { echo $before_title . $title . $after_title; }
		echo '<ul>';
		global $post;
		query_posts(array(
            'post_type' => 'listings',
            'order' => 'DSC',
			$taxonomy => $tag,
            'posts_per_page' => $number
		));

        if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

            <li>
                <div class="img-wrap columns left">

					<?php ct_status(); ?>
                    <?php ct_first_image_tn_left(); ?>
                </div>
                <div class="eight columns marL10 left">
                    <h6><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h6>
                    <p class="price"><strong><?php currency(); ?><?php listing_price(); ?></strong></p>
                    <p class="propinfo"><?php if( (get_post_meta($post->ID, "_ct_mileage", true)) != 0 ) { ?><?php echo get_post_meta($post->ID, "_ct_mileage", true); ?><?php } ?> <?php _e('miles', 'contempo'); ?><br />
<?php extcolor(); ?> / <?php intcolor(); ?></p>
                </div>
                    <div class="clear"></div>
            </li>

        <?php endwhile; endif; wp_reset_query(); ?>
		
		<?php echo '</ul>'; ?>

           <?php if($viewalltext) { ?>
               <p id="viewall"><a class="read-more right" href="<?php echo $viewalllink; ?>"><?php echo $viewalltext; ?> <em>&rarr;</em></a></p>
           <?php } ?>

		
		<?php echo $after_widget; ?>
    <?php
   }

   function update($new_instance, $old_instance) {
	   return $new_instance;
   }

   function form($instance) {

		$title = esc_attr($instance['title']);
		$number = esc_attr($instance['number']);
		$taxonomy = esc_attr($instance['taxonomy']);
		$tag = esc_attr($instance['tag']);
		$viewalltext = esc_attr($instance['viewalltext']);
		$viewalllink = esc_attr($instance['viewalllink']);
		?>
		<p>
		   <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','contempo'); ?></label>
		   <input type="text" name="<?php echo $this->get_field_name('title'); ?>"  value="<?php echo $title; ?>" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" />
		</p>
		<p>
            <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number:','contempo'); ?></label>
            <select name="<?php echo $this->get_field_name('number'); ?>" class="widefat" id="<?php echo $this->get_field_id('number'); ?>">
                <?php for ( $i = 1; $i <= 10; $i += 1) { ?>
                <option value="<?php echo $i; ?>" <?php if($number == $i){ echo "selected='selected'";} ?>><?php echo $i; ?></option>
                <?php } ?>
            </select>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('taxonomy'); ?>"><?php _e('Taxonomy:','contempo'); ?></label>
            <select name="<?php echo $this->get_field_name('taxonomy'); ?>" class="widefat" id="<?php echo $this->get_field_id('taxonomy'); ?>">
                <?php
				foreach (get_object_taxonomies( 'listings', 'objects' ) as $tax => $value) { ?>
                <option value="<?php echo $tax; ?>" <?php if($taxonomy == $tax){ echo "selected='selected'";} ?>><?php echo $tax; ?></option>
                <?php } ?>
            </select>
        </p>
        <p>
		   <label for="<?php echo $this->get_field_id('tag'); ?>"><?php _e('Tag:','contempo'); ?></label>
		   <input type="text" name="<?php echo $this->get_field_name('tag'); ?>"  value="<?php echo $tag; ?>" class="widefat" id="<?php echo $this->get_field_id('tag'); ?>" />
		</p>
        <p>
		   <label for="<?php echo $this->get_field_id('viewalltext'); ?>"><?php _e('View All Link Text','contempo'); ?></label>
		   <input type="text" name="<?php echo $this->get_field_name('viewalltext'); ?>"  value="<?php echo $viewalltext; ?>" class="widefat" id="<?php echo $this->get_field_id('viewalltext'); ?>" />
		</p>
        <p>
		   <label for="<?php echo $this->get_field_id('viewalllink'); ?>"><?php _e('View All Link URL','contempo'); ?></label>
		   <input type="text" name="<?php echo $this->get_field_name('viewalllink'); ?>"  value="<?php echo $viewalllink; ?>" class="widefat" id="<?php echo $this->get_field_id('viewalllink'); ?>" />
		</p>
		<?php
	}
}

register_widget('ct_Listings');
?>

Let’s start with simplifying the idea:

First where is the availability stored? In a custom form field (known to WP API as metadata)?
Second, there seems to be a lot of code here (a lot that wouldn’t be applicable to the problem), so are we primarily focusing on the following?

        global $post; 
        query_posts(array( 
            'post_type' => 'listings',  
            'order' => 'DSC', 
            $taxonomy => $tag, 
            'posts_per_page' => $number 
        )); 
             
        if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> 

Many thanks for the quick replay.

It is a yes to both of your questions. I have it set up so that there are custom fields where you can enter in aspects such as car colour etc. One of these is status, which is either filled in using sold or available. Also as you have highlighted the relevent bit of code. Just being fairly new to php i just wanted to make sure i did not include a relevant bit of data.

Try the following:

        global $post; 
        query_posts(array( 
            'post_type' => 'listings',  
            'order' => 'DSC', 
            $taxonomy => $tag, 
            'posts_per_page' => $number,
            'meta_query' = array(
               array(
                  'key' => 'status', // You will need to verify this is the name of your custom field
                  'value' => 'Available',
                  'compare' => '='
               ) 
            ) 
        )); 
             
        if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>  

If that doesn’t work, I believe this would:

        global $post; 
        query_posts(array( 
            'post_type' => 'listings',  
            'order' => 'DSC', 
            $taxonomy => $tag, 
            'posts_per_page' => $number,
            'meta_key' = 'status', // You will need to verify this is the name of your custom field
            'meta_value' => 'Available'
        )); 
             
        if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>  

I got this information from
http://codex.wordpress.org/Class_Reference/WP_Query

and

Many thanks for the help, was not expecting such a quick reply and solution. Unfortunately i’m getting a HTTP error 500 - so can’t upload the changes. I’ve contacted the provider about this issue as it has happens a few times with the new site.

If it’s not to much to ask the only other problem I’m trying to fix is how to default a form value to a particular choice. It’s a similar aspect in that currently the search form under status defaults to Any where as i would like it to be pre selected to available.

<label for="ct_status"><?php _e('Status', 'contempo'); ?></label>
                <?php ct_search_form_select('status'); ?>

I understand from the above code that ‘contempo’ is used to draw the relevant data from the value ‘Status’. I used the codex.wordpress link you supplied, to look up contempo but i seem to be back to trial and lots of error stage. Would it be a long the lines of including available within the brackets next to status or is it more to do with a value or true statement. Though this might be just backing up the fact that aspects of php are still a bit of a loss to me.

What theme/plugins are you using? As ct_search_form_select does not seem to be part of the WordPress core.

I found this searching online, but it isn’t obvious what we’d have to do to setup a default value (so I’m hoping the plugin/theme you are using may have documentation).

// Advanced Search Select
function ct_search_form_select($name, $taxonomy_name = null) {
        global $search_values;
       
        if (!$taxonomy_name) {
                $taxonomy_name = $name;
        }
        ?>
        <select id="ct_<?php echo $name; ?>" name="ct_<?php echo $name; ?>">
                <option value="0">Any</option>
                <?php foreach( get_terms($taxonomy_name, 'hide_empty=0') as $t ) : ?>
                        <?php if ($search_values[$name] == $t->slug) { $selected = 'selected="selected" '; } else { $selected = ''; } ?>
                        <option <?php echo $selected; ?>value="<?php echo $t->slug; ?>"><?php echo $t->name; ?></option>
                <?php endforeach; ?>
        </select>
        <?php
}
Edit:

I’m going to move this to the WordPress forum, as maybe you will get a bit more help there.

Apologies for the delay in any response in this thread. Finally managed to sort out the issue with the hosting so that i now have a website that is viewable online.

Thanks again for the help. I’ve managed to get the featured listing sorted so that it only displays the available cars. It’s now just the issue about getting the search form to start on a per-selected value as opposed to ‘any’. The theme i’m using is one that i purchased a while back. Looking into how the theme works, the search listing aspect this issue relates to is not a widget but just a stand alone php file which is embedded in the index php.

<div class="left">
                <label for="ct_status"><?php _e('Status', 'contempo'); ?></label>
                <?php ct_search_form_select('status'); ?>
            </div>

The only place I have found in the code where there is reference to status selected as ‘available’ is in the set up for the menu.

?ct_status=available&search-listings=true

Though - <?php ct_search_form_select(‘status=available’); ?> does not seem to be the simple easy solution.

Many thanks again for the replies and help.

Keith

And a quick follow up to say that some how i have managed to write a bit of code. Thought i would share the solution just in case anyone else gets stuck.

<div class="left">
                <label for="ct_status"><?php _e('Status', 'contempo'); ?></label>
                <?php ct_search_form_select('status'); ?>
            </div>

So as i understand it the code above - <?php ct_search_form_select(‘status’); ?> is looking for the different entries i have used in the status field and then populating the form with these. In this case it is either available and sold, with this addition of any as the first choice. From my aspect i wanted the form to default to available.

The solution below was to replace the line of code - <?php ct_search_form_select(‘status’); ?> with the below.

<select id="ct_status" name="ct_status">
                <option value="<?php _e('Available', 'contempo'); ?>"><?php _e('Available', 'contempo'); ?></option>
                <option value="<?php _e('Sold', 'contempo'); ?>"><?php _e('Sold', 'contempo'); ?></option>

Have to admit i’m amazing and delighted to final get this last element of the site working. Like an actor who says they are a horse rider after one time on a horse does this now mean i can add code writer on my cv - just joking, would be far to stressful having to do much more in the world of coding.