Home › Forums › General Issues › Query custom post type e custom field
Hi guys, I’ve a custom post type called issue_number with some custom fields including a field called year.
ok.
I want realize a query that it does this:
– a list of YEAR with every issue_number of that year.
YEAR X–>issue_number 1 and thumbnail – issue_number 2 and thumbnail – issue_number 3 and thumbnail
YEAR Y–>issue_number 4 and thumbnail – issue_number 5 and thumbnail -issue_number 6 and thumbnail
etc.
My code for moment is this :
`<?php
// args
$args = array(
‘numberposts’ => -1,
‘post_type’ => ‘issue_number’,
‘posts_per_page’ => 100,
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
But work in half because the result is :
YEAR X –> issue_number 1 and thumbnail
YEAR X –> issue_number 2 and thumbnail
Etc
thanks!
I resolved the problem adding a new custom post type called ANNO and 1 relationship field called anno_issue from year to issue_number.
The code is this for query :
` <?php
// args
$args = array(
‘numberposts’ => -1,
‘post_type’ => ‘anno’,
‘posts_per_page’ => 100,
);
// query
$the_query = new WP_Query( $args );
?>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
mh_before_page_content();
mh_magazine_page_title();?>
<?php
/*
* Query posts for a relationship value.
* This method uses the meta_query LIKE to match the string “123” to the database value a:1:{i:0;s:3:”123″;} (serialized array)
*/
$posts = get_posts(array(
‘post_type’ => ‘issue_number’, //nome post type da dove recupero le info
‘posts_per_page’ => -1,
‘meta_query’ => array(
array(
‘key’ => ‘anno_issue’, // nome custom field all’interno di post che mi da l’info
‘value’ => ‘”‘ . get_the_ID() . ‘”‘, // matches exaclty “123”, not just 123. This prevents a match for “1234”
‘compare’ => ‘LIKE’
)
)
));
?>
<?php /*ciclo stampa articoli */ ?>
<?php if( $posts ): ?>
<?php foreach( $posts as $post ): ?>
<?php /*layout 1 del tema per articoli */ ?>
<article >
—HTML CODE DISPLAYNG YOUR LOOP—
</article>
<?php /*fine layout 1 */ ?>
<?php endforeach; ?>
<?php endif; ?>
<?php /*fine ciclo stampa articoli */ ?>
<?php endwhile; // end of the loop. ?>
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 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.