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 giving Custom Query Fields a shot for filtering/sorting posts on the front-end of a site. The instructions seem pretty self-explanatory, but I do have another layer that I'm trying to figure out.

For one of my custom fields, it is currently storing a fractional number. I am controlling the input so only values like 3 3/4 or 1/5 are accepted (to not break any type of fraction format). This is done due to how the values need to be displayed on the site and the specificity needed (these are for product dimensions). Then, I am using a data attribute, run through the following function to convert the fraction to a decimal:

function fraction_to_decimal($input) {
    $fraction = array('whole' => 0);
    preg_match('/^((?P<whole>\d+)(?=\s))?(\s*)?(?P<numerator>\d+)\/(?P<denominator>\d+)$/', $input, $fraction);
    if (isset($fraction['denominator']) && isset($fraction['numerator'])) {
        $result = $fraction['whole'] + $fraction['numerator']/$fraction['denominator'];
        return $result;
    }
    else {
        return $input;
    }
}

The reason I am doing this on the front-end display and not in the admin is for two reasons:

  1. Simplicity for the user to enter (enter in a fraction as you normally would)
  2. Going from fraction to decimal suits my needs for filters using http://isotope.metafizzy.co/

The reason I am introducing Custom Query Fields into the mix is for my interest in removing Isotope from the setup and rely more on core WordPress filter/sort capabilities.

So - onto the actual question

Since my meta_values are stored as a "broken" integer, 3 3/5 until they are run through that function, is there a way to intercept the meta value interpretation of Custom Query Fields to get the decimal value instead? Thanks for any help!

share|improve this question

2 Answers 2

I've had a brief look at the plug-in, it offers no hooks to do this, but you could use posts_clauses hook to alter the query. That said my SQL is not good enough to say how (if it's possible) to convert the 'human readable' value to a decimal, prior to sorting.

Even if it could, my suggestion would be to store the value in a decimal format, and then convert it to 'human readable form' when displaying to the user.

share|improve this answer
    
Appreciate the input @Stephen - I'll look into posts_clauses a bit more. Thanks! –  Zach Jun 26 '12 at 14:51
up vote 0 down vote accepted

What I ended up doing was storing the data I needed in the post_meta table on-save:

add_action( 'save_post', 'mouldings_hidden_postmeta' );

function mouldings_hidden_postmeta() {
    global $post;
    if (is_admin() && (get_post_type() == 'moulding_profiles' || get_post_type() == 'moulding_combination')) {
        $mouldings_meta_keys = array(
            '_mouldings_dimensions_width',
            '_mouldings_dimensions_height',
            '_mouldings_projection_width',
            '_mouldings_projection_height'
        );
        foreach ($mouldings_meta_keys as $mouldings_meta_key) {
            if(get_post_type() == 'moulding_profiles' && ($mouldings_meta_key == '_mouldings_projection_width' || $mouldings_meta_key == '_mouldings_projection_height')) {
                continue;
            }
            if(!get_post_meta($post->ID, $mouldings_meta_key)) {
                add_post_meta($post->ID, $mouldings_meta_key, fraction_to_decimal(get_field(str_replace('_mouldings_', 'mp_profile_', $mouldings_meta_key))));
            }
            else {
                update_post_meta($post->ID, $mouldings_meta_key, fraction_to_decimal(get_field(str_replace('_mouldings_', 'mp_profile_', $mouldings_meta_key))));
            }
        }
    }
}
share|improve this answer

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.