Home › Forums › General Issues › get pages with same chexbox value
Hi,
I created a chexbox field named “categorie” with this options:
ceg : Correre e Giocare
vei : Vedere e Imparare
mes : Mangiare e Scoprire
I want to get all pages where option “ceg” is cheked but without result :((
my code:
<?php
$pagecat = get_pages('meta_key=categorie&meta_value=ceg');
if( $pagecat ) { ?>
<ul>
<?php foreach ($pagecat as $pagecats) { ?>
<li>
<a href="<?php echo get_page_link($pagecats->ID); ?>">
<?php echo get_the_title($pagecats->ID); ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
Thanks
F
Hi @donnafefe
Your code is close, but because the checkbox field saves it’s data as a serialized array, you will need to do a LIKE meta_query to find all posts with a checked value. This is documented here:
http://www.advancedcustomfields.com/resources/field-types/checkbox/
Thanks
E
I Eliot,
I tried but without result 🙁
I’m in page.php and after header e before footer I wrote:
<?php if (is_page('6')) { ?>
<?php
$pagecat = get_pages(array(
'meta_query' => array(
array(
'key' => 'categorie',
'value' => '"ceg"',
'compare' => 'LIKE'
)
)
));
if( $pagecat ) { ?>
<ul>
<?php foreach ($pagecat as $pagecats) { ?>
<li>
<a href="<?php echo get_page_link($pagecats->ID); ?>">
<?php echo get_the_title($pagecats->ID); ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php } else { ?>
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php } ?>
It show all pages but only one page in cms has “ceg” value checked.
where did I go wrong?
Hi @donnafefe
I have a feeling that the get_pages function does not have the same args as get_posts, and therefor the meta_query wont work.
Please change the get_pages to get_posts, and also include the 'post_type' => 'page'
param
Thanks
E
Hi Eliot
Thanks but nothing 🙁
I always used radiobutton, in fact I tried and it’s perfect but I need to be able to select more than one choice.
Hi @donnafefe
Huh? I don’t understand. My previous comment talked about the get_posts function, nothing about a radio button…
sorry eliot, my english is very bad and maybe I translate literally from italian 😛
I meant that i always used in the past radiobutton to chose one chance and it worked well but this time I need to be able to select more than one choice, so I have to use checkboxf field so previous comment are correct but the code doesn’t work 🙁
Hi @donnafefe
As said previously, please change the get_pages
to get_posts
, and also include the ‘post_type’ => ‘page’ param.
I think the issue with your code is that you are using get_pages
. If you change this to get_posts
, the issue will be solved
The topic ‘get pages with same chexbox value’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.