0

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?

  • Hi @Cycle99 - welcome to WPSE. Please familiarise yourself with WPSE best practices. I would also suggest editing your answer to show a specific problem you've encountered. If you would like to use wp_query to order posts by a meta value the codex will help you. Have a good read of this WP Codex: wp_query – Bysander May 27 at 11:18
  • @Bysander thx, but i'm familiar with the wp_query - but not within a foreach-loop like the example above. my question is how to order foreach loop results by meta. – Cycle99 May 27 at 11:46
0

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>';
}
| improve this answer | |
0

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.