Home › Forums › General Issues › sort posts by custom fields – bad data returned if function created
Hi,
I used example code from documentation:
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
And it works, Example output list:
0 – /
0 – Welcome tu Mercy / Beatus
100 – uzeum dusz czyśćcowych. Opowieści niesamowite. / uzeum dusz czyśćcowych. Opowieści niesamowite.
54 – 10×10 / 10×10
56 – Dom na skraju parku / La casa sperduta nel parco
60 – Lunatyczka / Sleepwalker
60 – Oblicze mroku / Look Away
66 – Cam / Cam
70 – Calibre / Calibre
70 – Mrok / The Dark
71 – Wpis Eksperymentalny / Experimental post
80 – Ostatnie dni Jacka Sparksa / The Last Days Of Jack Sparks
But when I put that code into function:
function list_genre_posts($genre){
//while ( have_posts() ) :
//the_post();
$posts = get_posts(array(
'posts_per_page' => -1,
'order' => 'ASC',
'post_type' => 'post',
'meta_key' => 'sumaryczna_ocena',
'orderby' => 'meta_value',
));
if( $posts ): ?>
<?php echo "<br><br>$genre<br>" ?>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<?php
$current_post_genre = get_field( "gatunek" );
/* if ( $current_post_genre == 'Thriller' ): ?>
<a href="<?php the_permalink(); ?>"><?php the_field('gatunek'); ?>: <?php the_field('nazwa_pl'); ?> - <?php the_field('rok_premiery'); ?></a><br>
<?php endif; ?> */?>
<a href="<?php the_permalink(); ?>"><?php the_field('sumaryczna_ocena'); ?> - <?php the_field('nazwa_pl'); ?> / <?php the_field('nazwa_oryginalu'); ?></a><br>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?><?php
//endwhile;
}
?>
It end with output:
Thriller
60 – Oblicze mroku / Look Away
60 – Oblicze mroku / Look Away
60 – Oblicze mroku / Look Away
60 – Oblicze mroku / Look Away
60 – Oblicze mroku / Look Away
60 – Oblicze mroku / Look Away
60 – Oblicze mroku / Look Away
60 – Oblicze mroku / Look Away
60 – Oblicze mroku / Look Away
60 – Oblicze mroku / Look Away
60 – Oblicze mroku / Look Away
60 – Oblicze mroku / Look Away
So it repeat only one post.
I tried (as seen in code) to add while loop as in single.php file, but then I had empty list.
Please advise how to put that code into function.
Ps. Is it possible to invoke that part of code after clikcing the button / link without changing site?
function list_genre_posts($genre){
/* ADD THIS */
global $post;
//while ( have_posts() ) :
//the_post();
$posts = get_posts(array(
'posts_per_page' => -1,
'order' => 'ASC',
'post_type' => 'post',
'meta_key' => 'sumaryczna_ocena',
'orderby' => 'meta_value',
));
if( $posts ): ?>
<?php echo "<br><br>$genre<br>" ?>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<?php
$current_post_genre = get_field( "gatunek" );
/* if ( $current_post_genre == 'Thriller' ): ?>
<a href="<?php the_permalink(); ?>"><?php the_field('gatunek'); ?>: <?php the_field('nazwa_pl'); ?> - <?php the_field('rok_premiery'); ?></a><br>
<?php endif; ?> */?>
<a href="<?php the_permalink(); ?>"><?php the_field('sumaryczna_ocena'); ?> - <?php the_field('nazwa_pl'); ?> / <?php the_field('nazwa_oryginalu'); ?></a><br>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?><?php
//endwhile;
}
?>
Hi,
I’d like to order posts and found the code how to do it. But where do I need to put that code?
(It’s about default blog posts)
Thank you!
The topic ‘sort posts by custom fields – bad data returned if function created’ 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.