Je souhaite ordonner mes posts wordpress à l'aide d'un get_posts() avec à la fois un…
- User
Je souhaite ordonner mes posts wordpress à l'aide d'un get_posts() avec à la fois un orderby par rapport à une taxonomy 'category-news' et par un champs acf 'ordre
- Assistant
Sure, I can help you with that! To order your WordPress posts using
get_posts()with both a taxonomy 'category-news' and an ACF field 'ordre', you can use themeta_keyandorderbyparameters in combination with a tax query. Here's an example:php$args = array( 'post_type' => 'post', // replace with your post type if necessary 'posts_per_page' => -1, // get all posts 'tax_query' => array( array( 'taxonomy' => 'category-news', 'field' => 'slug', 'terms' => 'your-category-slug', // replace with the slug of the category you want to filter by ), ), 'meta_key' => 'ordre', // replace with the name of your ACF field 'orderby' => array( 'meta_value_num' => 'ASC', // order by the numeric value of the ACF field in ascending order 'taxonomy' => 'category-news', // then order by the category-news taxonomy ), ); $posts = get_posts( $args );In this example, we're using a tax query to filter posts by the 'category-news' taxonomy with a specific slug. We're also using the
meta_keyparameter to specify the name of the ACF field we want to order by, and theorderbyparameter to order the posts first by the numeric value of the ACF field in ascending order, and then by the 'category-news' taxonomy.Make sure to replace 'your-category-slug' with the actual slug of the category you want to filter by, and 'ordre' with the name of your ACF field.
If you want to order the posts by the 'category-news' taxonomy first and then by the ACF field, you can simply switch the order of the keys in the
orderbyarray:php'orderby' => array( 'taxonomy' => 'category-news', // order by the category-news taxonomy first 'meta_value_num' => 'ASC', // then order by the numeric value of the ACF field in ascending order ),I hope this helps! Let me know if you have any questions.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.