Home › Forums › Front-end Issues › custom field "the_field" inside the loop array
Hi!
I like to create a custom loop to show custom post filterd by a custom field (checkbox). This works fine if a add the value for the custom field inside the code. This example works fine:
<?php
// args
$args = array(
'post_type' => 'produktark',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produkttyp',
'compare' => 'LIKE',
'value' => 'graveskuffer'
)
)
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
But i like to choose the value via a custom post field inside the page. So i can select on a page, witch custom posts filted by value i like to show. This custom field is also a checkbox on the page-admin. And now i try this:
<?php
// args
$args = array(
'post_type' => 'produktark',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produkttyp',
'compare' => 'LIKE',
'value' => the_field('vis_produktark_fra')
)
)
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
And it doesnt´t work. How can i use a custom field (checkbox) on page to create a custom loop filterd by a custom field value?
Thanks for help!
Markus
Hello, I have the same probleme.
Someone knows how to solve this, can’t find anything.
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
You must be logged in to reply to this topic.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.