Answer the question
In order to leave comments, you need to log in
How to merge records by custom field?
Hello. I want to get the following structure on the page:
Comedy
Title 1, Title 2, Title 3
Tragedy
Title 1, Title 4, Title5
Documentary
Title 2, Title 4, Title5
and so on.
Comedy, Tragedy and Documentary are arbitrary fields of records
. I cannot make them headings, as headings will be displayed differently in layout.
It is necessary to deduce Arbitrary fields.
My code
<?php
class Cinema_Walker_Category extends Walker_Category {
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
$name = $category->name;
$category_id = $category->term_id;
$count = $category->count;
$output .= $name;
$myPosts = get_posts(array(
'category' => $category->term_id,
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'cinema'
)
)
));
foreach($myPosts as $newPosts){
$newText = $newPosts->post_title;
$post_id = $newPosts->ID;
$permalink = get_permalink($post_id);
$thumb = get_the_post_thumbnail_url( $post_id, 'medium' );
$meta_values = get_post_meta( $post_id, 'cinema', true );
$string = '';
switch ($meta_values){
case 0:
$string = 'драма';
break;
case 1:
$string = 'комедия';
break;
case 2:
$string = 'трагедия';
break;
case 3:
$string = 'документалка';
break;
};
$output .= '<span class="floor-category__floor-name">'.$string.'</span><span>'.$newText.'</span>';
}
}
}
?>
Answer the question
In order to leave comments, you need to log in
Well, I see several ways. One of them is to sort the selection by the cinema field, then only dramas will go first, then comedies, etc. Well, then in the foric where $output is formed, form the necessary output. Toist first we display the names of the genre, then the name of the films. And next time display the name of the genre only if it has changed. I think this will be the most painless option.
Change the query to the following:
$myPosts = get_posts(array(
'category' => $category->term_id,
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'cinema',
'meta_query' => array(
array(
'key' => 'cinema'
)
)
));
$prev_value = -1;
if ($meta_values != $prev_value ) {
$prev_value = $meta_values;
$output = rtrim($output,', '); // Удалям последнюю запятую
$output .= '<br><span class="floor-category__floor-name">'.$string.'</span><br>'
}
$output .= '<span>'.$newText.'</span>, '
An arbitrary field .. it seems to be intended for another,
but the conclusion .. before it was solved like this
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->;term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id = 1,2,3
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->postmeta.meta_key = 'paragraf'
ORDER BY $wpdb->postmeta.meta_value ASC
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question