0

I'm facing a problem I can't resolve alone.

I have a custom meta field named 'processeur' for a custom post type names 'ordinateur'.

What I need to to is get a list of all meta values, arrange them in a checkboxes form so that the front user can make a query like this :

http://mywebsite.com/ordinateur/?processeur=xxx

I'm stuck in getting two things to work :

  1. Displaying automatically all meta values in input checkboxes.

  2. Having done that manually, I want the boxes to remain checked when the query is done. If 1 checkbox has been checked, it's fine, but then if two or more were checked, only gets the last value (yyy here) :

    http://mywebsite.com/ordinateur/?processeur=xxx&processeur=yyy

Here's my code for the checkboxes :

<form action="<?php $GET_['SERVER_URI'] ?> method="GET">
    <input multiple="multiple" type="checkbox" name="processeur" value="Core i3" />
    <input multiple="multiple" type="checkbox" name="processeur" value="Core i5" />
    <input multiple="multiple" type="checkbox" name="processeur" value="Core i7" />

        <input type="submit" name="submit" value="view" />

Here's my function for displaying alle meta values :

function generate_processeur_meta_keys(){
    global $wpdb;
    $post_type = 'ordinateur';
    $query = "
        SELECT DISTINCT($wpdb->postmeta.meta_key) 
        FROM $wpdb->posts 
        LEFT JOIN $wpdb->postmeta 
        ON $wpdb->posts.ID = $wpdb->postmeta.post_id 
        WHERE $wpdb->posts.post_type = '%s' 
        AND $wpdb->postmeta.meta_key != '' 
        AND $wpdb->postmeta.meta_key NOT RegExp '(^[_0-9].+$)' 
        AND $wpdb->postmeta.meta_key NOT RegExp '(^[0-9]+$)'
    ";
    $meta_keys = $wpdb->get_col($wpdb->prepare($query, $post_type));
    set_transient('processeur', $meta_keys); # create 1 Day Expiration
    return $meta_keys;
}

function get_processeur_meta_keys(){
    $cache = get_transient('processeur');
    $meta_keys = $cache ? $cache : generate_processeur_meta_keys();
    return $meta_keys;
}

But <?php get_processeur_meta_keys() ?> just displays "array()" without any values.

I'd be eternally thankful if you could help me, myself and my nerves !

Best

  • edit your question to add the code you use to render the form and to save values. – mmm Jan 15 '18 at 12:44
  • There. I currently did not include get_processeur_meta_keys() in dthe forme since this doesn't get me anything. – Samuel Haddad Jan 15 '18 at 12:54
  • to have multiple choices with checkboxes you need to set the names like name="processeur[]" – mmm Jan 15 '18 at 13:11
  • Do you want to have a list of meta keys, or meta values? I think you want the latter, which means the query would look more like SELECT $wpdb->postmeta.meta_value … GROUP BY $wpdb->postmeta.meta_key. Also, have you tried to run this SQL query directly in the database to see if you're getting any results? Have you deleted the transient before testing? During development I'd comment out the set_transient() call. Otherwise you're fetching an empty array from the array the whole time. – swissspidy Jan 15 '18 at 13:17
  • @mmm setting the name as "processeur[]" gives me this query : &processeur%5B%5D=Core+i3&processeur%5B%5D=Core+i5 Thus does not work.. – Samuel Haddad Jan 15 '18 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.