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.
intval
. What is the purpose of usingintval
here?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 getstrval
to work as it won't filter the string and I don't understand why.urldecode
the stringExecutive
does return a proper value, but isn't able to filter the results correctly. When selecting the dropdown and hitting the filter button, it returnspropertytype_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.