0

I am trying to do a query to grab all the pages that have the tickbox checked "feature_on_front_page" which has the value "featured" with label "Featured".

I added this checkbox with ACF.

My query is:

        <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
        <?php

        // args
        $args = array(
            'numberposts'   => -1,
            'post_type'     => 'page',
            'meta_key'      => 'feature_on_front_page',
            'meta_value'    => 'featured'
        );


        // query
        $the_query = new WP_Query( $args );

        ?>
        <?php if( $the_query->have_posts() ): ?>
            <ul>
                <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
                    <li>
                       do some stuff
                    </li>
                <?php endwhile; ?>
            </ul>
        <?php endif; ?>

        <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

It brings back 0 results. I looked in database and the "feature_on_front_page" key has data value "a:1:{i:0;s:8:"featured";}"

I have tried using array instead of string but I can't work out exactly what I am doing wrong

I tried:

'meta_value'    => array('Featured' => 'featured')

and

'meta_value'    => array('featured')

and various other variations. I know this is simple but I can't see where I am going wrong.

Here is what it looks like in database:

enter image description here

2 Answers 2

1

UPDATE: Ok this should work, according to ACF's documentation:

$args = array(
    'posts_per_page'   => -1,
    'post_type'     => 'page',
    'meta_query'    => array(
        array(
            'key'       => 'feature_on_front_page',
            'value'     => 'featured',
            'compare'   => 'LIKE'
        ),
    )
);

Your code works for me.

Can you see the feature_on_front_page custom field when you view the page(s) in the WordPress admin area?

Or perhaps the problem lies in the do some stuff section you've omitted?

7
  • Yes, I have gone into pages, ticked the box and then saved as ACF makes you create a fieldset so I am not sure if its nested somehow? I think the issue is the way the value is stored in database "a:1:{i:0;s:8:"featured";}" and not just a simple string but I am not sure.
    – Guerrilla
    Apr 26, 2015 at 20:23
  • Ah yes it looks as if ACF stores the value as a serialized array. Try 'meta_value' => 'a:1:{i:0;s:8:"featured";}' - pretty ugly hack I know :)
    – sqrbrkt
    Apr 26, 2015 at 20:30
  • Yeah this works so its obviously the way I'm accessing it. What is proper way as this doesn't appear in any example I have looked at
    – Guerrilla
    Apr 26, 2015 at 20:33
  • I'm not familiar with ACF but is there a way to specify that the custom field feature_on_front_page should be a simple boolean (true or false) rather than an array?
    – sqrbrkt
    Apr 26, 2015 at 20:35
  • See my update above
    – sqrbrkt
    Apr 26, 2015 at 20:40
-1

Try "posts_per_page" in place of "numberposts". numberposts is not a valid parameter of WP_Query.

if you are using "ACF", then I think this will not work with WP_Query. This is not logical but I had same kind of bad experience. Try once

2

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.