0

I'm trying to create a query returning pages with a certain meta value.

Only using meta_key works, it just doesn't work when I use both meta_key and meta_value.

I know the meta_value is correct - I have double checked several times. I'm using the Custom Fields plugin and I've tried both the name, value, even copy pasting the value... so it couldn't just be that I'm using the wrong value.

Any help is very much appreciated!

Here's my query:

$skolor_query = new WP_Query(array(
'post_type' => 'page',
'meta_key' => 'program_skola',
'meta_value' => 'es'
));

if ($skolor_query->have_posts()) { 
        ?>

    <ul>
        <?php while ( $skolor_query->have_posts() ) : $skolor_query->the_post();            

            $skola = get_the_title();

            ?>
            <li><?php echo $skola; ?></li>

            <?php endwhile;
                } 
             ?>

        </ul>

Here's the custom field: Custom field

$q = new WP_Query( $args );
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();

    ?><pre><?php var_dump( get_post_meta( get_the_ID(), 'program_skola' ) ); ?></pre><?php

}
wp_reset_postdata();
}

Returns:

array (size=1)
  0 => 
array (size=2)
  0 => string 'sp' (length=2)
  1 => string 'tp' (length=2)

array (size=1)
  0 => 
array (size=3)
  0 => string 'ek' (length=2)
  1 => string 'es' (length=2)
  2 => string 'nv' (length=2)

array (size=1)
  0 => 
array (size=5)
  0 => string 'ek' (length=2)
  1 => string 'es' (length=2)
  2 => string 'nv' (length=2)
  3 => string 'sp' (length=2)
  4 => string 'tp' (length=2)
6
  • I know the meta_value is correct. It can't be correct if you are not getting anything back. Exactly how are values saved. File an edit and paste either a row from the db or an image of a custom field row. Commented Nov 30, 2015 at 7:51
  • I added an image of the custom field.
    – Erika
    Commented Nov 30, 2015 at 8:04
  • That does not say much unfortunately, except that there are 5 values to the custom field. If you save all 5 values (as a string or an array) under the same custom field for each post, your query will never work. Best to save one value per custom field Commented Nov 30, 2015 at 8:12
  • Why will it never work? Sorry, it might be a stupid question, but I'm not very experienced with programming and this is the first time I'm working with custom fields as well.
    – Erika
    Commented Nov 30, 2015 at 8:26
  • It seems I can get the values (es, tp, etc) from get_post_meta() so I think I can find an an ugly solution to display the pages.
    – Erika
    Commented Nov 30, 2015 at 8:43

1 Answer 1

0

I'm still not really sure how the values from your custom field is saved into the db, but your format in db does not match up with the format you are querying from meta_value. From your image it seems that you are saving 5 value under one custom field or in the format of es: xxxxxx which will never match es explicitely

To debug this, or to be really sure how your values are stored, run the following query and note the output from your custom field. You should take it from there then

$args = [
    'post_type' => 'page',
    'meta_key'  => 'program_skola'
];
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
    $q->the_post();

        ?><pre><?php var_dump( get_post_meta( get_the_ID(), 'program_skola' ) ); ?></pre><?php

    }
    wp_reset_postdata();
}

EDIT

From your var_dump() from get_post_meta():

array (size=5)
  0 => string 'ek' (length=2)
  1 => string 'es' (length=2)
  2 => string 'nv' (length=2)
  3 => string 'sp' (length=2)
  4 => string 'tp' (length=2)

This is an array of values that is stored as a custom field single value. It is sad to say, but when you need custom field data for sorting or for searching functionality, you cannot use serialized data (values which are stored as arrays are serialized before storing) as it will never work (as you have seen). In general, and for no-problems-in-future approach, a custom field should be used to store only a single value for searching and ordering purposes

You should look to create a custom field for each and every value, and then use value like 0 and 1 as triggers. For instance, a post can have a field es with a value of either 0 or no or a value of 1 or yes. The other custom fields will be ek, nv etc as needed, each field with the value described. If you need to query posts with a custom field of es and that was marked as 1 (which will mean that, from what I can see, that the course is needed), you can query it as follow

$args = [
    'post_type'  => 'page',
    'meta_key'   => 'es',
    'meta_value' => 1
];
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
    $q->the_post();

        the_title();

    }
    wp_reset_postdata();
}

As for your current setup, the only thing I can quickly come up with is the following

$args = [
    'post_type' => 'page',
    'meta_key'  => 'program_skola'
];
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
    $q->the_post();

        $meta = get_post_meta( get_the_ID(), 'program_skola' ) ); 
        if ( in_array( 'es', $meta ) ) {

            // Display posts with value 'es'
            the_title();

         }

    }
    wp_reset_postdata();
}

It should be noted that this is not very good for performance as you are querying posts which you do not need, which wastes time and db calls

7
  • I get something but I don't quite understand it. I will add the output to my question.
    – Erika
    Commented Nov 30, 2015 at 8:31
  • All that I understand is that it doesn't look like it should.
    – Erika
    Commented Nov 30, 2015 at 8:35
  • See my edit. Hope that helps a bit Commented Nov 30, 2015 at 9:16
  • Thank you so much for your time! It helps a lot :) I tried the second approach before seeing your answer, and well - it worked but as you say, it's not at all ideal. And I have to use the values in other contexts as well so I'll need to try the "one per field" approach instead.
    – Erika
    Commented Nov 30, 2015 at 9:45
  • 1
    It works using the one per field approach so I'll go for one query and different arguments depending on the page. :) That should do it.
    – Erika
    Commented Nov 30, 2015 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.