Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

What I am attempting to do is sort posts by custom fields.

I have a few custom fields that don't query properly and I get the 404 page. The fields that don't work I used hyphens in the naming convention, i.e. var-name. When I switch the hyphens to underscores it works, i.e. var_name.

Am I overlooking something simple in PHP or Wordpress that causes conflicts when I do this?

  • I know I could simply just switch everything to underscores but I like to know the root of issues so I can avoid them in the future.

I am using pre_get_posts to modify the query being executed:

add_action( 'pre_get_posts', 'my_modify_query' );
function my_modify_query( $wp_query ) {

    if ( is_category() ) {

        // Works - underscore
        if ( isset( $_REQUEST['var_name'] ) ) {
            $meta_query[] = array( 'key' => 'var_name', 'value' => $_REQUEST['var_name'] );
        }

        // Sometimes works - hyphen
        if ( isset( $_REQUEST['var-name'] ) ) {
            $meta_query[] = array( 'key' => 'var-name', 'value' => $_REQUEST['var-name'] );
        }

        if ( ! empty( $meta_query ) ) {
            $wp_query->set( 'meta_query', $meta_query );
        }

    }
}

Have additional custom fields I check for, the above example is for brevity and clarification and is the same for additional fields.

EDIT 1

Here are the queries being run by Wordpress:

The unsuccessful hyphen

SELECT SQL_CALC_FOUND_ROWS  wp_xxxxx_posts.ID 
FROM wp_xxxxx_posts 
WHERE 1=1 
AND 0 = 1 
AND wp_xxxxx_posts.post_type IN ('post') 
AND (
    wp_xxxxx_posts.post_status = 'publish' 
    OR 
    wp_xxxxx_posts.post_author = 1 
    AND 
    wp_xxxxx_posts.post_status = 'private') 
GROUP BY wp_xxxxx_posts.ID 
ORDER BY wp_xxxxx_posts.post_date DESC 
LIMIT 0, 10

The successful underscore

SELECT SQL_CALC_FOUND_ROWS wp_xxxxx_posts.ID 
FROM wp_xxxxx_posts 
INNER JOIN wp_j9L3vi_postmeta ON (wp_xxxxx_posts.ID = wp_xxxxx_postmeta.post_id) 
WHERE 1=1 
AND wp_xxxxx_posts.post_type IN ('post', 'custom_post_type' ) 
AND (
    wp_xxxxx_posts.post_status = 'publish' 
    OR 
    wp_xxxxx_posts.post_author = 1 AND wp_xxxxx_posts.post_status = 'private'
) 
AND ( (
        wp_xxxxx_postmeta.meta_key = 'var_name' 
        AND CAST(wp_xxxxx_postmeta.meta_value AS CHAR) = '116'
) ) 
GROUP BY wp_xxxxx_posts.ID 
ORDER BY wp_xxxxx_posts.post_date DESC 
LIMIT 0, 10

If I go into phpMyAdmin and simply switch out var_name in the above query to var-name it works.

Edit 2

I am passing the custom fields as so:

http://mysite.com/?cat=example&var-name=100
share|improve this question
Either your example isn't really your code, or there's something else wrong, because $meta_query[] = array( 'key' => 'var_name', 'value' => $_REQUEST['var_name']; can't work. – kaiser Jan 23 at 23:52
Sure it does, just not for the hyphenated variable name. meta_query for the WP_Query object is an array of arrays. codex.wordpress.org/Class_Reference/… -> Multiple Custom Field Handling. Then I use the set method, set( $query_var, $value ), to change the request. – hungerstar Jan 24 at 2:52
I think there might be a conflict with a custom taxonomy name and the variable name through the rewrite rules in the database. – hungerstar Jan 24 at 3:12
No, it doesn't. You forgot the closing bracket for the array before the semi colon. This must break - no alternative to this scenario. – kaiser Jan 24 at 8:38
Okay, not sure how the closing parenthesis got removed above but it is there in my code, I'll update the example above. – hungerstar Jan 24 at 19:22

1 Answer

You're missing an array in there, $meta_query should be an array of arrays as follows:

$meta_query[] = array( array( 'key' => 'var-name', 'value' => $_REQUEST['var-name'] ) );

WordPress and MySQL are both happy with meta keys with underscores, hyphens or both in their name.

share|improve this answer
doh! ignore me, i just noticed the [] ... – Simon Blackbourn Jan 27 at 0:52

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.