• Resolved mYrAn

    (@myran)


    Hi!

    Im wondering if there’s a way to sort custom fields after the way you put them in (meta_id) and not randomly? Im using a function to write out a menu with weekdays, and it’s not very good to have fridag in front of monday..

    <?php if ( $keys = get_post_custom_keys() ) {
    foreach ( (array) $keys as $key ) {
    $keyt = trim($key);
    if ( '_' == $keyt{0} || 'cycle' == $keyt || 'pdf' == $keyt || 'youtube' == $keyt || 'underrubrik' == $keyt )
    continue;
    $values = array_map('trim', get_post_custom_values($key));
    $value = implode($values,', ');
    echo apply_filters('the_meta_key', "
    <div class='row'><div class='left'>$key</div><div class='right'>$value</div><!-- /right --></div><!-- /row -->
    ", $key, $value);
    }} ?>

    Thats the code im using. If there would be a way to sort them after meta_id, or choose which order to put them that would be great. Thanks a lot in advance!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The meta ID is not returned in the data you get back, there is no sufficient way to sort that data based on what you’re given from that function, other than manually sorting it into the correct order.

    If you want to sort them manually, how would you propose the sort order is determined?

    Thread Starter mYrAn

    (@myran)

    So there is no way to fetch that data? I dont care how i get them in the order i put them in, aslong as a client can do it, so Not to hard.

    A custom query to the database would likely be required to retrieve the meta ID.

    Eg.

    $mydata = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE post_id = X");
    // Where X should be the applicable post ID

    Of course the query probably needs more specifity than that, it was an example only.

    See here for writing custom queries to the WordPress database.
    http://codex.wordpress.org/Function_Reference/wpdb_Class

    Thread Starter mYrAn

    (@myran)

    If you boule help me compile the function i would be really grateful! I know almost no php 🙁

    If you could show you’ve tried first, that might help convince me.. 😉

    Thread Starter mYrAn

    (@myran)

    Well, i got absolutely nowhere. I dont even know if it got the right values since i couldnt echo, it just sayed array and i didnt know which variable to try and echo 🙁 Secoundly I have absolutely no idea how to order them, if i somehow got the meta_id part right, i tried using ORDER BY that i found in one of the examples but i really couldnt make any sense of how it worked..

    <?php
    $mydata = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE post_id = the_ID() ORDER BY ASC");
    // Where X should be the applicable post ID
    if ( $keys = get_post_custom_keys($mydata) ) {
    foreach ( (array) $keys as $key ) {
    $keyt = trim($key);
    if ( '_' == $keyt{0} || 'cycle' == $keyt )
    continue;
    $values = array_map('trim', get_post_custom_values($key));
    $value = implode($values,', ');
    echo apply_filters('the_meta_key', "
    <div class='row'><div class='left'>$key</div><div class='right'>$value</div><!-- /right --></div><!-- /row -->
    ", $key, $value);
    }} ?>
    <?php echo $mydata['meta_id']; ?>

    Ok, i’ll help you out.. 🙂

    What meta keys do you want to select that are associated with the post, any, all, select ones? I see you were excluding some in your code, are you wanting all minus those ones?

    Can you tell specifically what meta data you want back, or provide some details about how to determine which data you’re wanting to pull from the database, anything that will help me refine that query to pull only the data you want.

    Thread Starter mYrAn

    (@myran)

    Oh, thank you!
    The theme is built up like this:
    A post acts like a page for a show, containing info, pictures, youtube videos and an image slider. Everything on it besides the content and title is essentially controlled by custom fields. So on every page there is a slider, this infofield that i need to get in order, optional youtube video and optional pdf button, all which are controlled by custom fields. The easiest way would be to exclude the otherones, since those are “static” while the menu can be dynamic, it doesnt allways include the same stuff. So what i want is to order Every custom field except; cycle, pdf, youtube, underrubrik, in the way i put typed them in in the admin panel. So if i were to type in key Friday value Bananas and then key Saturday value Apples i want Friday to be the first to be outputted, saturday secound, sunday monday etc etc. And i want it to do this to all but the fields i mentioned above, it doesnt matter which order they come in 🙂 Ill provide more info if you need it, this is what i could make out right now!

    The code that follows the query is only an example, so you can see how the data looks printed out, modify/adjust as you need.

    $myid = 128; // Assign this using a variable, or however you were before, eg. $post->ID
    
    $data = $wpdb->get_results("
    SELECT * FROM $wpdb->postmeta WHERE post_id = $myid
    AND meta_key NOT LIKE '\_%'
    AND meta_key NOT IN('cycle','pdf','youtube','underrubrik')
    ORDER by meta_id ASC
    ");
    
    if( !empty( $data ) ) {
    	foreach( $data as $result_object ) {
    		print $result_object->meta_id;
    		print '<br />';
    		print $result_object->post_id;
    		print '<br />';
    		print $result_object->meta_key;
    		print '<br />';
    		print $result_object->meta_value;
    		print '<hr />';
    	}
    }

    If you’re confused about anything, just let me know.. 😉

    Thread Starter mYrAn

    (@myran)

    Omg that works perfectly, and i can actually make some sence out of some bits 🙂 This is what i ended up with, just editing the html and removing the uneccecary bits 🙂 Thank you So so much!

    <?php $myid = $post->ID; // Assign this using a variable, or however you were before, eg. $post->ID
                            $data = $wpdb->get_results("
                            SELECT * FROM $wpdb->postmeta WHERE post_id = $myid
                            AND meta_key NOT LIKE '\_%'
                            AND meta_key NOT IN('cycle','pdf','youtube','underrubrik')
                            ORDER by meta_id ASC
                        ");
    
                        if( !empty( $data ) ) {
                        foreach( $data as $result_object ) {
                        print '<div class="row"><div class="left">';
                        print $result_object->meta_key;
                        print '</div><div class="right">';
                        print $result_object->meta_value;
                        print '</div><!-- /right --></div><!-- /row -->';
                        }
                        } ?>

    Looks good(apart from the reformatted indentation), but happy to hear it’s all working for you and you’re welcome… 😉

    Yikes I have been struggling over this same sort issue for weeks now. I didn’t realize the results are not necessarily in the order they were entered.

    Thanks for this posting. It give me a starting point. Can I somehow use this function where it “defaults” to the current post? (i.e. I don’t need to specifically assign a postID/

    Thanks

    Hello

    I have found this post and it works excellent. I would have another question (maybe for Mark)…

    How to change meta_key name when print on screen. Let say, I have meta_key ad_new_book, and on screen i would like to print only book. I have many custom fields, so most of them start for example with ad_new_ .

    Thank you for your replay.

    Or if someone can help me by this to make sort output:
    [code moderated]
    Thank you all.

    Wow this is great! thanks guys

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Sort custom fields after meta_id, not randomly’ is closed to new replies.