Skip Navigation

[Résolu] Custom post order

This support ticket is created Il y a 6 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par marc-andre Il y a 6 années et 9 mois.

Assisté par: Christian Cox.

Auteur
Publications
#612472

Here is what i whant

i have create a custom taxonomy called : type-de-partenaire
it like gold-silver-bronze (level of parnership)

and i had create a custom post type : partenaire (partnership)

I what to sort partenaire by type-de-partenaire
so here is my code

$partenaires->query( array(
'post_type'=>'partenaire',
'posts_per_page'=> '-1',
'orderby', 'meta_value',
'meta_key', 'type-de-partenaire'
) );
--
here is the result.... all the second level, shold be regroup ad the order of the first level
lien caché
now the first on is Jean-Coutu -> platine, and should be Honda -> emerites

#612541

Hi, meta_key and meta_value represent custom fields, not taxonomy terms. It is not possible to sort a WordPress query by taxonomy term. See the documentation for WP_Query Orderby here:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
There are no options to sort WP_Query by a taxonomy term, because it is not possible. Instead, you could create a custom field with 3 options - gold, silver, and bronze. Then sort using this custom field instead of the taxonomy. If a taxonomy term is required on your site for some other reason, then you must use custom code to sync the selected term and custom field whenever the post is created or updated.

#613121

Here is my final code that work as ask... i whant to share it with the world

//======================================================================
add_shortcode( 'list_taxonomy', 'list_taxonomy' );
function list_taxonomy( $atts ) {

$output = '';

$taxonomy = $atts['taxonomy'];
$type = $atts['type'];

//get the taxonomy terms
$terms = get_terms( $taxonomy );
foreach ($terms as $term){ echo $term->name.'<br/>'; }
echo '<hr><br/>';

//compteur
//$nb = wp_count_posts('ressource');
//mam_dump($nb->publish);

foreach ($terms as $term){
$args = array(
'post_type' => $type,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
),
),
);

//query
$list = new WP_Query( $args );

$cpt=1;

$output .= '<h2>'.$term->name.'</h2>';

//output
while ($list->have_posts()) : $list->the_post();

$list_image = get_the_post_thumbnail_url();
$list_titre = get_the_title('');
$list_content = get_the_content('');
$list_contentfilter = apply_filters('the_content',$list_content);
//$partenaire_categorie = get_the_term_list( $partenaires->ID , 'type-de-partenaire' );
//$partenaire_categorie = strip_tags( $partenaire_categorie );
//$list_categorie = get_the_terms( $partenaires->ID, 'type-de-partenaire' );

$output .= $cpt.'-'.$list_titre.'<br/>';
$cpt++;

endwhile;

$output .= '<br/>';

}

wp_reset_postdata();

return $output;
}