in a cpt i can check some values (checkboxes) in a metabox. i can access this values by
$meta = get_post_meta(get_the_ID(), '_custom-meta-box', true );
after that, i do a foreach loop:
foreach ($meta as $key11 => $value11) {
if($meta[$key11] > 1) {
$my_postid11 = $value11;
$funktion11 = get_post_meta($my_postid11, 'funktion', true );
echo '<a href="'.get_the_permalink($my_postid11).'">'.get_the_title($my_postid11).' <div class="arrow"></div></a>';
} }
works fine so far. but: i don't have a clue to order them by my needs like in usual wp_query like
$args=array('order'=>'asc','orderby'=>'wpse_last_word' );
All i need is to order the results in the foreach loop by "wpse_last_word" or by title..
Any ideas?
in a cpt i can check some values (checkboxes) in a metabox. i can access this values by
$meta = get_post_meta(get_the_ID(), '_custom-meta-box', true );
after that, i do a foreach loop:
foreach ($meta as $key11 => $value11) {
if($meta[$key11] > 1) {
$my_postid11 = $value11;
$funktion11 = get_post_meta($my_postid11, 'funktion', true );
echo '<a href="'.get_the_permalink($my_postid11).'">'.get_the_title($my_postid11).' <div class="arrow"></div></a>';
} }
works fine so far. but: i don't have a clue to order them by my needs like in usual wp_query like
$args=array('order'=>'asc','orderby'=>'wpse_last_word' );
All i need is to order the results in the foreach loop by "wpse_last_word" or by title..
Any ideas?
Try below code to filter the data by title in Ascending Order:
$meta = get_post_meta(get_the_ID(), '_custom-meta-box', true );
$title_array = array();
foreach ($meta as $key11 => $value11) {
if($meta[$key11] > 1) {
$my_postid11 = $value11;
$title = get_the_title($my_postid11);
$id = $my_postid11;
$title_array[$id] = $title;
}
}
asort($title_array);
foreach ($title_array as $key22 => $value22) {
$my_postid22 = $key22;
$funktion11 = get_post_meta($my_postid22, 'funktion', true );
echo '<a href="'.get_the_permalink($my_postid22).'">'.$value22.' <div class="arrow"></div></a>';
}
If you are trying to order an array (what you are iterating over in a foreach loop) by something you are finding out within the loop itself then it's not going to be possible without first gaining that information.
One way of doing that would be to have a foreach loop beforehand to gain this information.
$array = [ 1, 8, 12, 16 ];
foreach ($array as $key => $entry) {
unset($array[$key]);
$array[$key]['value'] = $entry;
$array[$key]['title'] = uniqid(); // This could be your title (
}
Returns
Array
(
[0] => Array
(
[value] => 1
[title] => 5ecfb2ee803fa
)
[1] => Array
(
[value] => 8
[title] => 5ecfb2ee803fd
)
[2] => Array
(
[value] => 12
[title] => 5ecfb2ee803fe
)
[3] => Array
(
[value] => 16
[title] => 5ecfb2ee803ff
)
)
So you can then order the array using PHP Sort Functions For Arrays to order by 'title'
Where I was pointing you to in my comment would be that it is entirely possible to do this instead with wp_query
If you are already looking up posts and post data and meta - you can do it in one go.
If your $meta = get_post_meta(get_the_ID(), '_custom-meta-box', true );
is getting an array of post IDs.
$meta = get_post_meta(get_the_ID(), '_custom-meta-box', true );
$args = array(
'post_type' => 'my_custom_post_type',
'orderby' => 'tite',
'order' => 'ASC',
'post__in' => $meta,
);
Would be your query. You would then cycle through the loop like that to retrieve your titles, permalinks etc.