WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Omit orphaned postmeta values from wpdb query (3 posts)

  1. iamshawnrice
    Member
    Posted 11 months ago #

    Hi.

    Please take a look at http://drimproperties.com/property-search-2/

    I am using the following code to generate the bedroom select input:

    <?php
    	$metakey = 'bedrooms';
            $bedrooms = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
    
    	if ($bedrooms) {
    	foreach ($bedrooms as $bedroom) {
    	$bedStr = str_replace(".","", $bedroom);
    	echo "<option value=\"" . $bedStr . "\">" . $bedroom . "</option>";
    		}
    	}
    ?>

    Many of these metakey values (namely all of the .5's) are orphaned from deleted/edited posts. As such I would like to omit them from the query.

    Does anyone have insight on how to accomplish this?

    Thanks.

  2. bcworkz
    Member
    Posted 11 months ago #

    I'm pretty weak with SQL queries, but perhaps adding something like the following to the WHERE clause?
    AND CHAR_LENGTH(meta_value)<3

  3. iamshawnrice
    Member
    Posted 11 months ago #

    Worked like a charm! Thanks.

Reply

You must log in to post.

About this Topic

Tags