Home › Forums › Front-end Issues › custom field "the_field" inside the loop array › Reply To: custom field "the_field" inside the loop array
I apologize in advance if I’m not understanding your question correctly.
So you have a custom post type (produktark) with a checkbox field in it. Then on another page, you have a ACF to select which checkbox value from the CPT you want to query by?
Assuming that vis_produktark_fra is the field key for the field on the page you want to display the custom posts on, then you should be able to use get_field()
rather than the_field()
. the_field echos the value, which you don’t want to do.
So assuming I’m understanding you correctly, your code could look like:
<?php
$vis_produktark_fra = get_field('vis_produktark_fra');
// args
$args = array(
'post_type' => 'produktark',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produkttyp',
'compare' => 'LIKE',
'value' => $vis_produktark_fra
)
)
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
OR
<?php
// args
$args = array(
'post_type' => 'produktark',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produkttyp',
'compare' => 'LIKE',
'value' => get_field('vis_produktark_fra')
)
)
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
this is basically the same thing in my first code snipped
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!
🚀 ACF & ACF PRO 6.0.7 are now available.
— Advanced Custom Fields (@wp_acf) January 18, 2023
✨This release contains bug fixes and improvements while we continue to work on the next major release of ACF.https://t.co/wQgAOpwmUI
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.