i am using wp job manager plugin and i am trying to list on the frontpage only the twelve job categories according to the jobs they having. The job categories having more jobs go first. I managed to list the job categories with the above code but the problem is that the sorting and the number of categories not working as i expected. Any suggestions?
<?php echo '<pre>' . print_r( get_terms( ['taxonomy' => 'job_listing_category',] ), true ) . '</pre>'; ?>
<?php echo '<ul class="job-category-list">'; ?>
<?php foreach ( get_terms( array(
'taxonomy' => 'job_listing_category',
) ) as $cat):
?>
<?php $the_query = new WP_Query( array(
'post_type' => 'job_listing',
'post_status' => 'publish',
'posts_per_page' => '12',
'orderby' => 'tag_count',
'tax_query' => array(
array(
'taxonomy' => 'job_listing_category',
'field' => 'term_id',
'terms' => $cat->term_id
)
)
) );
$count = $the_query->found_posts;
?>
<?php echo '<li><a href="'.get_term_link($cat).'" title="'.esc_attr($cat->name).'">'.$cat->name.'</a> ('.( $count ).')</li>';
?>
<?php endforeach; ?>
<?php echo '</ul>'; ?>`
The output from get_terms()
:
Array
(
[0] => WP_Term Object
(
[term_id] => 3178
[name] => Αγορές / Προμήθεις
[slug] => agores-promithies
[term_group] => 0
[term_taxonomy_id] => 3168
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 7
[filter] => raw
)
[1] => WP_Term Object
(
[term_id] => 1125
[name] => Αγροτικά
[slug] => agrotika
[term_group] => 0
[term_taxonomy_id] => 126
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 7
[filter] => raw
)
[2] => WP_Term Object
(
[term_id] => 1127
[name] => Αερομεταφορές
[slug] => aerometafores
[term_group] => 0
[term_taxonomy_id] => 127
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
[3] => WP_Term Object
(
[term_id] => 1128
[name] => Αισθητική / Κομμωτήρια
[slug] => aisthitiki-komotiria
[term_group] => 0
[term_taxonomy_id] => 128
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 15
[filter] => raw
)
[4] => WP_Term Object
(
[term_id] => 1130
[name] => Ανθρώπινοι Πόροι
[slug] => antropinoi-poroi
[term_group] => 0
[term_taxonomy_id] => 129
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 18
[filter] => raw
)
[5] => WP_Term Object
(
[term_id] => 1129
[name] => Ανώτατη Διοίκηση
[slug] => anotati-dioikisi
[term_group] => 0
[term_taxonomy_id] => 130
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 13
[filter] => raw
)
[6] => WP_Term Object
(
[term_id] => 1131
[name] => Αποθήκη / Logistics
[slug] => logistics
[term_group] => 0
[term_taxonomy_id] => 131
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 44
[filter] => raw
)
[7] => WP_Term Object
(
[term_id] => 1132
[name] => Ασφάλειες
[slug] => asfaleies
[term_group] => 0
[term_taxonomy_id] => 132
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
[8] => WP_Term Object
(
[term_id] => 1133
[name] => Γραμματειακή Υποστήριξη
[slug] => secretary
[term_group] => 0
[term_taxonomy_id] => 133
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 85
[filter] => raw
)
[9] => WP_Term Object
(
[term_id] => 1135
[name] => Δημιουργικό Τμήμα / Γραφίστες
[slug] => dimiourgiko-tmima
[term_group] => 0
[term_taxonomy_id] => 134
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 16
[filter] => raw
)
[10] => WP_Term Object
(
[term_id] => 1134
[name] => Δημόσιες Σχέσεις
[slug] => dimosies-sxeseis
[term_group] => 0
[term_taxonomy_id] => 135
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 14
[filter] => raw
)
[11] => WP_Term Object
(
[term_id] => 1136
[name] => Δημοσιογράφοι / Μ.Μ.Ε.
[slug] => dimosiografoi
[term_group] => 0
[term_taxonomy_id] => 136
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 11
[filter] => raw
)
[12] => WP_Term Object
(
[term_id] => 1137
[name] => Διαφήμιση / Promotion
[slug] => diafimisi-promotion
[term_group] => 0
[term_taxonomy_id] => 137
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 21
[filter] => raw
)
[13] => WP_Term Object
(
[term_id] => 1138
[name] => Διαχείριση Ποιότητας
[slug] => diaxeirisi-poiotitas
[term_group] => 0
[term_taxonomy_id] => 138
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 10
[filter] => raw
)
[14] => WP_Term Object
(
[term_id] => 1139
[name] => Διοίκηση Επιχειρήσεων
[slug] => dioikisi-epixeiriseon
[term_group] => 0
[term_taxonomy_id] => 139
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 16
[filter] => raw
)
[15] => WP_Term Object
(
[term_id] => 1140
[name] => Εθελοντικές Εργασίες
[slug] => ethelontikes-ergasies
[term_group] => 0
[term_taxonomy_id] => 140
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 8
[filter] => raw
)
[16] => WP_Term Object
(
[term_id] => 1141
[name] => Εκπαιδευτικοί
[slug] => ekpedeutikoi
[term_group] => 0
[term_taxonomy_id] => 141
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 52
[filter] => raw
)
[17] => WP_Term Object
(
[term_id] => 1166
[name] => Εξυπηρέτηση Πελατών / Call Center
[slug] => call-center
[term_group] => 0
[term_taxonomy_id] => 142
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 30
[filter] => raw
)
[18] => WP_Term Object
(
[term_id] => 1168
[name] => Εξωτερικές Εργασίες / Security
[slug] => security
[term_group] => 0
[term_taxonomy_id] => 143
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 13
[filter] => raw
)
[19] => WP_Term Object
(
[term_id] => 1142
[name] => Επαγγέλματας Υγείας
[slug] => epagelmata-ygeias
[term_group] => 0
[term_taxonomy_id] => 145
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 43
[filter] => raw
)
[20] => WP_Term Object
(
[term_id] => 1143
[name] => Επιστήμες
[slug] => epistimes
[term_group] => 0
[term_taxonomy_id] => 146
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 20
[filter] => raw
)
[21] => WP_Term Object
(
[term_id] => 1144
[name] => Επιχειρηματικότητα
[slug] => epixeirimatikotita
[term_group] => 0
[term_taxonomy_id] => 147
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[22] => WP_Term Object
(
[term_id] => 1338
[name] => Εστιατόρια / Καφέ
[slug] => estiatoria-cafe
[term_group] => 0
[term_taxonomy_id] => 1340
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 96
[filter] => raw
)
[23] => WP_Term Object
(
[term_id] => 1145
[name] => Καταστήματα Λιανικής
[slug] => katastimata-lianikis
[term_group] => 0
[term_taxonomy_id] => 148
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 30
[filter] => raw
)
[24] => WP_Term Object
(
[term_id] => 1146
[name] => Λογιστήριο
[slug] => logistirio
[term_group] => 0
[term_taxonomy_id] => 149
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 23
[filter] => raw
)
[25] => WP_Term Object
(
[term_id] => 1147
[name] => Μάρκετινγκ
[slug] => marketing
[term_group] => 0
[term_taxonomy_id] => 150
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 23
[filter] => raw
)
[26] => WP_Term Object
(
[term_id] => 1148
[name] => Μεστικά
[slug] => mesitika
[term_group] => 0
[term_taxonomy_id] => 151
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 4
[filter] => raw
)
[27] => WP_Term Object
(
[term_id] => 1149
[name] => Μεταφορές
[slug] => metafores
[term_group] => 0
[term_taxonomy_id] => 152
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 31
[filter] => raw
)
[28] => WP_Term Object
(
[term_id] => 1150
[name] => Μηχανικοί
[slug] => mixanikoi
[term_group] => 0
[term_taxonomy_id] => 153
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 50
[filter] => raw
)
[29] => WP_Term Object
(
[term_id] => 1151
[name] => Ναυτιλιακά
[slug] => nautiliaka
[term_group] => 0
[term_taxonomy_id] => 154
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[30] => WP_Term Object
(
[term_id] => 1152
[name] => Νομικό Τμήμα
[slug] => nomiko-tmima
[term_group] => 0
[term_taxonomy_id] => 155
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[31] => WP_Term Object
(
[term_id] => 1165
[name] => Οδηγοί
[slug] => odigoi
[term_group] => 0
[term_taxonomy_id] => 156
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 40
[filter] => raw
)
[32] => WP_Term Object
(
[term_id] => 1167
[name] => Οικιακοί Βοηθοί
[slug] => oikiakoi-voithoi
[term_group] => 0
[term_taxonomy_id] => 157
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 11
[filter] => raw
)
[33] => WP_Term Object
(
[term_id] => 1153
[name] => Περιβάλλον
[slug] => perivalon
[term_group] => 0
[term_taxonomy_id] => 158
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[34] => WP_Term Object
(
[term_id] => 1154
[name] => Πληροφορική
[slug] => pliroforiki
[term_group] => 0
[term_taxonomy_id] => 159
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 28
[filter] => raw
)
[35] => WP_Term Object
(
[term_id] => 1155
[name] => Πολιτιστικά / Τέχνες
[slug] => politistika-texnes
[term_group] => 0
[term_taxonomy_id] => 160
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 12
[filter] => raw
)
[36] => WP_Term Object
(
[term_id] => 1156
[name] => Πωλήσεις
[slug] => poliseis
[term_group] => 0
[term_taxonomy_id] => 161
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 84
[filter] => raw
)
[37] => WP_Term Object
(
[term_id] => 1157
[name] => Σύμβουλοι Επιχειρήσεων
[slug] => symvouloi-epixeiriseon
[term_group] => 0
[term_taxonomy_id] => 162
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 9
[filter] => raw
)
[38] => WP_Term Object
(
[term_id] => 1158
[name] => Τεχνικοί / Συντηρητές
[slug] => texnikoi-syntirites
[term_group] => 0
[term_taxonomy_id] => 163
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 32
[filter] => raw
)
[39] => WP_Term Object
(
[term_id] => 1159
[name] => Τηλεπικοινωνίες
[slug] => tilepikoinonies
[term_group] => 0
[term_taxonomy_id] => 164
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[40] => WP_Term Object
(
[term_id] => 1160
[name] => Τομέας Παραγωγής
[slug] => tomeas-paragogis
[term_group] => 0
[term_taxonomy_id] => 165
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 21
[filter] => raw
)
[41] => WP_Term Object
(
[term_id] => 1161
[name] => Τουρισμός / Ξενοδοχεία
[slug] => tourismos-hotels
[term_group] => 0
[term_taxonomy_id] => 166
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 84
[filter] => raw
)
[42] => WP_Term Object
(
[term_id] => 1162
[name] => Τράπεζες
[slug] => trapezes
[term_group] => 0
[term_taxonomy_id] => 167
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 9
[filter] => raw
)
[43] => WP_Term Object
(
[term_id] => 1164
[name] => Υποστηρικτικοί Υπάλληλοι
[slug] => ypostiriktikoi-ypaliloi
[term_group] => 0
[term_taxonomy_id] => 168
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 63
[filter] => raw
)
[44] => WP_Term Object
(
[term_id] => 1163
[name] => Χρηματοοικονομικά
[slug] => xrimatooikonomika
[term_group] => 0
[term_taxonomy_id] => 169
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 14
[filter] => raw
)
)
i am using wp job manager plugin and i am trying to list on the frontpage only the twelve job categories according to the jobs they having. The job categories having more jobs go first. I managed to list the job categories with the above code but the problem is that the sorting and the number of categories not working as i expected. Any suggestions?
<?php echo '<pre>' . print_r( get_terms( ['taxonomy' => 'job_listing_category',] ), true ) . '</pre>'; ?>
<?php echo '<ul class="job-category-list">'; ?>
<?php foreach ( get_terms( array(
'taxonomy' => 'job_listing_category',
) ) as $cat):
?>
<?php $the_query = new WP_Query( array(
'post_type' => 'job_listing',
'post_status' => 'publish',
'posts_per_page' => '12',
'orderby' => 'tag_count',
'tax_query' => array(
array(
'taxonomy' => 'job_listing_category',
'field' => 'term_id',
'terms' => $cat->term_id
)
)
) );
$count = $the_query->found_posts;
?>
<?php echo '<li><a href="'.get_term_link($cat).'" title="'.esc_attr($cat->name).'">'.$cat->name.'</a> ('.( $count ).')</li>';
?>
<?php endforeach; ?>
<?php echo '</ul>'; ?>`
The output from get_terms()
:
Array
(
[0] => WP_Term Object
(
[term_id] => 3178
[name] => Αγορές / Προμήθεις
[slug] => agores-promithies
[term_group] => 0
[term_taxonomy_id] => 3168
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 7
[filter] => raw
)
[1] => WP_Term Object
(
[term_id] => 1125
[name] => Αγροτικά
[slug] => agrotika
[term_group] => 0
[term_taxonomy_id] => 126
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 7
[filter] => raw
)
[2] => WP_Term Object
(
[term_id] => 1127
[name] => Αερομεταφορές
[slug] => aerometafores
[term_group] => 0
[term_taxonomy_id] => 127
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
[3] => WP_Term Object
(
[term_id] => 1128
[name] => Αισθητική / Κομμωτήρια
[slug] => aisthitiki-komotiria
[term_group] => 0
[term_taxonomy_id] => 128
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 15
[filter] => raw
)
[4] => WP_Term Object
(
[term_id] => 1130
[name] => Ανθρώπινοι Πόροι
[slug] => antropinoi-poroi
[term_group] => 0
[term_taxonomy_id] => 129
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 18
[filter] => raw
)
[5] => WP_Term Object
(
[term_id] => 1129
[name] => Ανώτατη Διοίκηση
[slug] => anotati-dioikisi
[term_group] => 0
[term_taxonomy_id] => 130
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 13
[filter] => raw
)
[6] => WP_Term Object
(
[term_id] => 1131
[name] => Αποθήκη / Logistics
[slug] => logistics
[term_group] => 0
[term_taxonomy_id] => 131
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 44
[filter] => raw
)
[7] => WP_Term Object
(
[term_id] => 1132
[name] => Ασφάλειες
[slug] => asfaleies
[term_group] => 0
[term_taxonomy_id] => 132
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
)
[8] => WP_Term Object
(
[term_id] => 1133
[name] => Γραμματειακή Υποστήριξη
[slug] => secretary
[term_group] => 0
[term_taxonomy_id] => 133
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 85
[filter] => raw
)
[9] => WP_Term Object
(
[term_id] => 1135
[name] => Δημιουργικό Τμήμα / Γραφίστες
[slug] => dimiourgiko-tmima
[term_group] => 0
[term_taxonomy_id] => 134
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 16
[filter] => raw
)
[10] => WP_Term Object
(
[term_id] => 1134
[name] => Δημόσιες Σχέσεις
[slug] => dimosies-sxeseis
[term_group] => 0
[term_taxonomy_id] => 135
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 14
[filter] => raw
)
[11] => WP_Term Object
(
[term_id] => 1136
[name] => Δημοσιογράφοι / Μ.Μ.Ε.
[slug] => dimosiografoi
[term_group] => 0
[term_taxonomy_id] => 136
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 11
[filter] => raw
)
[12] => WP_Term Object
(
[term_id] => 1137
[name] => Διαφήμιση / Promotion
[slug] => diafimisi-promotion
[term_group] => 0
[term_taxonomy_id] => 137
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 21
[filter] => raw
)
[13] => WP_Term Object
(
[term_id] => 1138
[name] => Διαχείριση Ποιότητας
[slug] => diaxeirisi-poiotitas
[term_group] => 0
[term_taxonomy_id] => 138
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 10
[filter] => raw
)
[14] => WP_Term Object
(
[term_id] => 1139
[name] => Διοίκηση Επιχειρήσεων
[slug] => dioikisi-epixeiriseon
[term_group] => 0
[term_taxonomy_id] => 139
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 16
[filter] => raw
)
[15] => WP_Term Object
(
[term_id] => 1140
[name] => Εθελοντικές Εργασίες
[slug] => ethelontikes-ergasies
[term_group] => 0
[term_taxonomy_id] => 140
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 8
[filter] => raw
)
[16] => WP_Term Object
(
[term_id] => 1141
[name] => Εκπαιδευτικοί
[slug] => ekpedeutikoi
[term_group] => 0
[term_taxonomy_id] => 141
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 52
[filter] => raw
)
[17] => WP_Term Object
(
[term_id] => 1166
[name] => Εξυπηρέτηση Πελατών / Call Center
[slug] => call-center
[term_group] => 0
[term_taxonomy_id] => 142
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 30
[filter] => raw
)
[18] => WP_Term Object
(
[term_id] => 1168
[name] => Εξωτερικές Εργασίες / Security
[slug] => security
[term_group] => 0
[term_taxonomy_id] => 143
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 13
[filter] => raw
)
[19] => WP_Term Object
(
[term_id] => 1142
[name] => Επαγγέλματας Υγείας
[slug] => epagelmata-ygeias
[term_group] => 0
[term_taxonomy_id] => 145
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 43
[filter] => raw
)
[20] => WP_Term Object
(
[term_id] => 1143
[name] => Επιστήμες
[slug] => epistimes
[term_group] => 0
[term_taxonomy_id] => 146
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 20
[filter] => raw
)
[21] => WP_Term Object
(
[term_id] => 1144
[name] => Επιχειρηματικότητα
[slug] => epixeirimatikotita
[term_group] => 0
[term_taxonomy_id] => 147
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[22] => WP_Term Object
(
[term_id] => 1338
[name] => Εστιατόρια / Καφέ
[slug] => estiatoria-cafe
[term_group] => 0
[term_taxonomy_id] => 1340
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 96
[filter] => raw
)
[23] => WP_Term Object
(
[term_id] => 1145
[name] => Καταστήματα Λιανικής
[slug] => katastimata-lianikis
[term_group] => 0
[term_taxonomy_id] => 148
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 30
[filter] => raw
)
[24] => WP_Term Object
(
[term_id] => 1146
[name] => Λογιστήριο
[slug] => logistirio
[term_group] => 0
[term_taxonomy_id] => 149
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 23
[filter] => raw
)
[25] => WP_Term Object
(
[term_id] => 1147
[name] => Μάρκετινγκ
[slug] => marketing
[term_group] => 0
[term_taxonomy_id] => 150
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 23
[filter] => raw
)
[26] => WP_Term Object
(
[term_id] => 1148
[name] => Μεστικά
[slug] => mesitika
[term_group] => 0
[term_taxonomy_id] => 151
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 4
[filter] => raw
)
[27] => WP_Term Object
(
[term_id] => 1149
[name] => Μεταφορές
[slug] => metafores
[term_group] => 0
[term_taxonomy_id] => 152
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 31
[filter] => raw
)
[28] => WP_Term Object
(
[term_id] => 1150
[name] => Μηχανικοί
[slug] => mixanikoi
[term_group] => 0
[term_taxonomy_id] => 153
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 50
[filter] => raw
)
[29] => WP_Term Object
(
[term_id] => 1151
[name] => Ναυτιλιακά
[slug] => nautiliaka
[term_group] => 0
[term_taxonomy_id] => 154
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[30] => WP_Term Object
(
[term_id] => 1152
[name] => Νομικό Τμήμα
[slug] => nomiko-tmima
[term_group] => 0
[term_taxonomy_id] => 155
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[31] => WP_Term Object
(
[term_id] => 1165
[name] => Οδηγοί
[slug] => odigoi
[term_group] => 0
[term_taxonomy_id] => 156
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 40
[filter] => raw
)
[32] => WP_Term Object
(
[term_id] => 1167
[name] => Οικιακοί Βοηθοί
[slug] => oikiakoi-voithoi
[term_group] => 0
[term_taxonomy_id] => 157
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 11
[filter] => raw
)
[33] => WP_Term Object
(
[term_id] => 1153
[name] => Περιβάλλον
[slug] => perivalon
[term_group] => 0
[term_taxonomy_id] => 158
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[34] => WP_Term Object
(
[term_id] => 1154
[name] => Πληροφορική
[slug] => pliroforiki
[term_group] => 0
[term_taxonomy_id] => 159
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 28
[filter] => raw
)
[35] => WP_Term Object
(
[term_id] => 1155
[name] => Πολιτιστικά / Τέχνες
[slug] => politistika-texnes
[term_group] => 0
[term_taxonomy_id] => 160
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 12
[filter] => raw
)
[36] => WP_Term Object
(
[term_id] => 1156
[name] => Πωλήσεις
[slug] => poliseis
[term_group] => 0
[term_taxonomy_id] => 161
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 84
[filter] => raw
)
[37] => WP_Term Object
(
[term_id] => 1157
[name] => Σύμβουλοι Επιχειρήσεων
[slug] => symvouloi-epixeiriseon
[term_group] => 0
[term_taxonomy_id] => 162
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 9
[filter] => raw
)
[38] => WP_Term Object
(
[term_id] => 1158
[name] => Τεχνικοί / Συντηρητές
[slug] => texnikoi-syntirites
[term_group] => 0
[term_taxonomy_id] => 163
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 32
[filter] => raw
)
[39] => WP_Term Object
(
[term_id] => 1159
[name] => Τηλεπικοινωνίες
[slug] => tilepikoinonies
[term_group] => 0
[term_taxonomy_id] => 164
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[40] => WP_Term Object
(
[term_id] => 1160
[name] => Τομέας Παραγωγής
[slug] => tomeas-paragogis
[term_group] => 0
[term_taxonomy_id] => 165
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 21
[filter] => raw
)
[41] => WP_Term Object
(
[term_id] => 1161
[name] => Τουρισμός / Ξενοδοχεία
[slug] => tourismos-hotels
[term_group] => 0
[term_taxonomy_id] => 166
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 84
[filter] => raw
)
[42] => WP_Term Object
(
[term_id] => 1162
[name] => Τράπεζες
[slug] => trapezes
[term_group] => 0
[term_taxonomy_id] => 167
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 9
[filter] => raw
)
[43] => WP_Term Object
(
[term_id] => 1164
[name] => Υποστηρικτικοί Υπάλληλοι
[slug] => ypostiriktikoi-ypaliloi
[term_group] => 0
[term_taxonomy_id] => 168
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 63
[filter] => raw
)
[44] => WP_Term Object
(
[term_id] => 1163
[name] => Χρηματοοικονομικά
[slug] => xrimatooikonomika
[term_group] => 0
[term_taxonomy_id] => 169
[taxonomy] => job_listing_category
[description] =>
[parent] => 0
[count] => 14
[filter] => raw
)
)
Share
Improve this question
edited Mar 30, 2020 at 20:27
disinfor
3662 silver badges9 bronze badges
asked Mar 29, 2020 at 16:09
PanosPanos
391 silver badge10 bronze badges
3
- 1 You have job categories and you want the categories ordered by the number of jobs per category? – disinfor Commented Mar 30, 2020 at 18:06
- Yes exactly. Ι have job categories and i want to order them according to the number of jobs that they having. – Panos Commented Mar 30, 2020 at 18:36
- dear panos dear disinfor - many thanks for this great idea - perhaps this is a great contribution for the snippet collection - see link below wpjobmanager/customization-snippets This page consists of all the customization snippets we have so far. We may occasionally add new ones and we encourage everyone to also send their own snippets to us! If you add a customization that you think will benefit others, get in touch with the guys at wpjobmanager/customization-snippets have a great day – zero Commented Apr 27, 2020 at 21:51
1 Answer
Reset to default 1Edited:
This is a much simpler version of what you are looking to do. You don't need WP_Query()
. You can use a foreach
loop and an iterator:
<?php
echo '<ul class="job-category-list">';
// Get the terms and order them by count in descending order.
$terms = get_terms( [
'taxonomy' => 'job_listing_category',
'orderby' => 'count',
'order' => 'DESC',
]
);
// Set an iterator
$i = 1;
// foreach term
foreach ( $terms as $term ) {
// if the iterator is less than or equal to 12.
if ( $i <= 12 ) :
echo '<li><a href="' . get_term_link( $term->term_id ) . '" title="' . esc_attr( $term->name ) . '">' . $term->name . '</a> (' . ( $term->count ) . ')</li>';
// Increase the iterator.
$i++;
endif;
}
echo '<ul><!-- /job-category-list -->';
?>
For the count, not sure why are you seeing a difference. But this essentially gives you what you want.
EDIT: from this wpse post: List taxonomy / category count showing list published posts only
You can use this filter to get the correct count for only published posts:
function get_terms_filter_published( $terms, $taxonomies, $args ) {
global $wpdb;
$taxonomy = $taxonomies[0];
if ( ! is_array($terms) && count($terms) < 1 ) {
return $terms;
}
$filtered_terms = array();
$ctr = 0;
foreach ( $terms as $term ) {
$result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts p JOIN $wpdb->term_relationships rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = $term->term_id AND p.post_status = 'publish' LIMIT 1");
$published_terms[ $ctr ] = $term;
if ( intval($result) > 0 ) {
$published_terms[ $ctr ] = $term;
} else {
// you can comment this out if you don't want to show empty terms
$published_terms[ $ctr ]->count = 0;
}
$ctr++;
}
return $published_terms;
}
add_filter('get_terms', 'get_terms_filter_published', 10, 3);
So, add that filter function to your functions.php and use the code above to get what you want - only the first 12 categories based on post count.