Home › Forums › Front-end Issues › WP_query to order by custom field
Hello here!
I’ve got one custom taxonomy archive page named taxonomy-serie.php
This is supposed to list all the figures I have related to a serie.
Example : on /the-legend-of-zelda I can see figures like “Zelda”, “Link” or “Ganon”.
My first attempt was that :
<?php if (have_posts()) : ?>
<ul class="flex cards">
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'medium' );?></a>
<h2><a href="<?php the_permalink(); ?>">
<?php the_title(); ?><br>
</a>
</h2>
Série :<?php the_terms( $post->ID, 'serie', '' ); ?><br>
<?php
$today = new DateTime();
$date_fr = DateTime::createFromFormat('Ymd', get_field('release_date_fr'));
?>
...
That is fine. BUT I couldn’t order figures by release date which is a custom field from ACF.
So I try to do something with WP_query :
// query the figures
$the_query = new WP_Query(array(
'post_type' => 'figure',
'posts_per_page' => -1,
'meta_key' => 'release_date_fr',
'orderby' => 'meta_value',
'order' => 'DESC',
));
?>
<?php
// starting loop
if( $the_query->have_posts() ): ?>
<ul class="flex cards">
<?php while( $the_query->have_posts() ) : $the_query->the_post();
$class = get_field('release_date_fr') ? 'class="release_date_fr"' : '';
...
It partially work. Ordering was right, but I got all figures, from all series…
I was wondering if I have to add a tax_query, but all my attempts are bad.
// filter on figures from serie's taxonomy
$myquery['tax_query'] = array(
array(
'taxonomy' => 'serie',
'field' => 'slug',
),
);
// query the figures
$the_query = new WP_Query($myquery(
'post_type' => 'figure',
'posts_per_page' => -1,
'meta_key' => 'release_date_fr',
'orderby' => 'meta_value',
'order' => 'DESC',
));
?>
<?php
// starting loop
if( $the_query->have_posts() ): ?>
<ul class="flex cards">
<?php while( $the_query->have_posts() ) : $the_query->the_post();
$class = get_field('release_date_fr') ? 'class="release_date_fr"' : '';
?>
...
Error 500… Could you please help me ?
If your figures connected to taxonomies, then you can filter by tax_query
Your query has to look like this:
// filter on figures from serie's taxonomy
$args = array(
'post_type' => 'figure',
'posts_per_page' => -1,
'meta_key' => 'release_date_fr',
'orderby' => 'meta_value',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'serie', // taxonomy name
'field' => 'slug', // term_id, slug or name
'terms' => array( 'the-legend-of-zelda', 'hyrule-warriors' ), // single value or array of term id(s), term slug(s) or term name(s)
)
)
);
// query the figures
$the_query = new WP_Query($args);
BTW: If there is a error 500, then there is a new entry in you error logs, with more details about the error. Always start at this point to look for a solution.
Thank you wdj-pascal it works. I just added a variable with the current term and that’s ok!
The topic ‘WP_query to order by custom field’ 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.