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.

I'm been racking my head at this for a while now, and I can't figure out how to fix the code.

Here's the issue;

I found a snippet of code online to filter my post results in edit.php based on predetermined info. I've already modified the code to fit the needs of my wordpress theme. In this case, it's Property Types.

Predetermined values being '1 Bedroom, 2 Bedroom, 3 Bedroom, Bachelor, Executive'

The values are entered through the theme options, and the dropdown with filter options DO work however, it will only filter the values that have an integer. (ie. 1 Bedroom, 2, Bedroom, etc) Bachelor, Executive, etc will not filter correctly.

Here is the original code:

functions.php

function restrict_articles_by_propertytype_value() {
    global $wpdb;
    $propertytype_values = $wpdb->get_col("
        SELECT DISTINCT meta_value
        FROM ". $wpdb->postmeta ."
        WHERE meta_key = 'propertytype_value'
        ORDER BY meta_value
    ");
    ?>
    <select name="propertytype_value_restrict_articles" id="propertytype_value">
        <option value="">Show All Property Types</option>
        <?php foreach ($propertytype_values as $propertytype_value) { ?>
        <option value="<?php echo esc_attr( $propertytype_value ); ?>" <?php if(isset($_GET['propertytype_value_restrict_articles']) && !empty($_GET['propertytype_value_restrict_articles']) ) selected($_GET['propertytype_value_restrict_articles'], $propertytype_value); ?>>
        <?php

          echo $propertytype_value;
        ?>
        </option>
        <?php } ?>
    </select>
    <?php
}
add_action('restrict_manage_posts','restrict_articles_by_propertytype_value');

Followed by the function to filter the results on the table.

functions.php

function posts_where( $where ) {
    if( is_admin() ) {
        global $wpdb;       
if ( isset( $_GET['propertytype_value_restrict_articles'] ) && !empty( $_GET['propertytype_value_restrict_articles'] ) && intval( $_GET['propertytype_value_restrict_articles'] ) != 0 ) {
            $propertytype_value_number = intval( $_GET['propertytype_value_restrict_articles'] );

            $where .= " AND ID IN (SELECT post_id FROM " . $wpdb->postmeta ." 
WHERE meta_key='propertytype_value' AND meta_value=$propertytype_value_number )";
        }
}   
    return $where;
}
add_filter( 'posts_where' , 'posts_where' );

I can see the cause being the function intvar(as seen below) but I can't get the filter to work with strvar or a different method as I'm still very new to PHP development.

I need the filter to work even if there is no number/integer value present.

Been looking around google for hours here, and I can't seem to find the answer. I'm probably missing something very trivial.

share|improve this question
1  
vpropertytype_value_restrict_articles` holds the various filter options as strings? Then you've already spotted the problem - you are using intval. What is the purpose of using intval here? –  vancoder Apr 1 '13 at 22:56
    
These filters are being used for multiple columns, to filter additional fields such as Bedrooms, Bathrooms, etc. intval seems to be the only function that returns a proper value for filtering values that have a meta_value that's an integer. I can't get strval to work as it won't filter the string and I don't understand why. –  Nima Moradi Apr 1 '13 at 23:36
    
"intval seems to be the only function that returns a proper value for filtering" - but it isn't returning a proper value if 'Executive' doesn't work, is it? I think you can drop intval completely and just use the string directly. The posts_where filter should sanitize it. –  vancoder Apr 1 '13 at 23:50
    
If there is an issue with the space, you may need to urldecode the string –  vancoder Apr 1 '13 at 23:56
    
Executive does return a proper value, but isn't able to filter the results correctly. When selecting the dropdown and hitting the filter button, it returns propertytype_value_restrict_articles=Executive but the table isn't able to filter the results. I don't think there's an issue with the space, it seems to only want to accept the filter if the value is an integer and not a string. –  Nima Moradi Apr 2 '13 at 0:01
show 2 more comments

1 Answer

With some trial and error and help from @vancoder , the problem had to do with intval.

The only section needing to be modified was the posts_where function.

Had to remove the following line:

$propertytype_value_number = intval( $_GET['propertytype_value_restrict_articles'] );

And change:

AND meta_value=$beds_value_number )";

to

AND meta_value='".$_GET['propertytype_value_restrict_articles']."' )";

I had to also modify the original function and remove the && intval( ) !=0 argument in order for it to work.

Here is the revised working code for filtering meta_value from a custom string.

function.php

function posts_where( $where ) {
    if( is_admin() ) {
        global $wpdb;       
if ( isset( $_GET['propertytype_value_restrict_articles'] ) && !empty( $_GET['propertytype_value_restrict_articles'] )) {
            $where .= " AND ID IN (SELECT post_id FROM " . $wpdb->postmeta ." 
WHERE meta_key='propertytype_value' AND meta_value='".$_GET['propertytype_value_restrict_articles']."' )";
        }
}   
    return $where;
}
add_filter( 'posts_where' , 'posts_where' );
share|improve this answer
    
Answer pulled from OP. –  brasofilo Sep 22 '13 at 5:38
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.