0

I'm making one page website and I added a custom field on "Page" post type that asks "Use this page as a section:" Yes or No(with dropdown). I called all pages on template-onepage.php(custom template) using wp query

<?php $args = array(
    'post_type' => 'pages',
    'order' => 'ASC'
);
$the_query = new WP_Query($args);
?>
<?php if (have_posts()) : while (the_query->have_posts()): the_query->the_posts ?>
    <?php some content here ?>
<?php endwhile; endif; ?>

Now what I want is to call only those page that set "Use this page as a section" to yes.. How can I achieve it ??? Please help.. Thanks

1 Answer 1

0

You need to add a meta query, which will specifically select those posts that have both the meta key you want, and the desired value.

$args = array(
    'post_type' => 'pages',
    'order' => 'ASC',
    'meta_query' => array(
     array(
        'key'     => 'use-this-page-as-a-section', // or whatever the key is
        'value'   => 'yes'
     )
 );
5
  • Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Mar 13, 2015 at 23:00
  • Added as requested.
    – user12479
    Mar 13, 2015 at 23:09
  • 'key' is the slug of the field? right?
    – Hussy571
    Mar 14, 2015 at 7:12
  • Ok I got it now thanks for your help.. Thank you very much..
    – Hussy571
    Mar 14, 2015 at 8:01
  • @Hussy571 yes, "key" is the slug of the field.
    – user12479
    Mar 14, 2015 at 11:45

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.