Support » Fixing WordPress » alphabetize custom fields the_meta

  • Resolved TheElear

    (@peterpap)


    In my twenty ten child theme I have some information for each post displayed using Custom Fields. These are things like date of composition or length of a piece, etc.

    I pull them on the category archive page using template function <?php the_meta(); ?> placed in the loop just under the post title. Works absolutely fine.

    This function for each custom field pulls its key and corresponding value.
    For example:
    Date: 1970
    Duration: 7′

    I have 4 custom fields for each post. Lets call them A, B, C, D.
    When they are displayed they are unsorted:
    B
    D
    A
    C
    or whatever. I am not able to determine what sorting it is.

    I am looking for a solution to have them alphabetically sorted so the list looks like that:
    A: value a
    B: value b
    C: value c
    etc.

    Is it a piece of code I should enter in the () brackets of the the_meta() function?

    Any help will be much appreciated.
    Peter

Viewing 4 replies - 1 through 4 (of 4 total)
  • I’m having the same problem – just posted a question (oops!)

    I have just one custom field, school name, and I want the output ordered alphabetical ascending and have tweaked the sql but nope – doesn’t want it. Hope somebody can help us

    Thread Starter TheElear

    (@peterpap)

    wow! I have some results.

    I studied these 2 threads:
    1 http://wordpress.org/support/topic/how-to-sort-custom-fields-alphabetically-by-key
    2 http://wordpress.org/support/topic/sort-custom-fields-after-meta_id-not-randomly

    I could not make in my twenty ten child the 1st work. So started to look into number 2.

    And got some results.

    The original code is:

    <?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 $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 />';
    	}
    }
    ?>

    As I only want meta_key followed by meta_value e.g.:
    Date: 1973 I made some modifications, see below:

    <?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 '\_%'
    ORDER by meta_key ASC
    ");
    
    	if( !empty( $data ) ) {
    		foreach( $data as $result_object ) {
    			print $result_object->meta_key;
    			print ': ';
    			print $result_object->meta_value;
    			print '<br />';
    					}
    				}
    		?>

    and it WORKS!
    See my test page here: http://coelo.eu/category/test-category-1/

    I strugle however to apply formatting. In the thread no 2, mentioned above, a user adds <div> tags to change her formatting.
    I already have styles defined for both the key and the value in my .css. However when I added div’s I just cannot make it work. My classes are as follows:

    .post-meta {list-style: none; font-weight: normal; padding: 0px; margin: 0px;}
    .post-meta-key {font-weight: bold; margin: 0px;}

    Any ideas how to modify this:

    print $result_object->meta_key;
    print ': ';
    print $result_object->meta_value;
    print '<br />';

    so it looks like that:
    Date: 1973
    Education: blablabla

    I am so glad I made some progress on it!
    Now just to finish it off 🙂

    Thread Starter TheElear

    (@peterpap)

    ok. In order to sort and format custom field alphabetically as below:
    Angle: 30 degrees
    Birthday: 124
    Education: higher
    Key3: whatever

    I have used the following code in the loop.php:

    <div class="pprow">
    <?php $myid = $post->ID; 
    
    $data = $wpdb->get_results("
    SELECT * FROM $wpdb->postmeta WHERE post_id = $myid
    AND meta_key NOT LIKE '\_%'
    ORDER by meta_key ASC
    ");
    
    	if( !empty( $data ) ) {
    		foreach( $data as $result_object ) {
    			print '<div class="ppmeta-key">';
    			print $result_object->meta_key;
    			print ': ';
    			print '</div><div class="ppmeta-value">';
    			print $result_object->meta_value;
    			print '</div>';
    			print '<br />';
    						}
    				}
    	?>
    	</div><!-- .pprow -->

    and the following styling in .css:

    .pprow {
    	margin-bottom: 24px;
    	}
    
    .ppmeta-key {
    	font-weight: bold;
    	margin: 0px;
    	display: inline
    	}
    
    .ppmeta-value {
    	list-style: none;
    	font-weight: normal;
    	padding: 0px;
    	margin: 0px;
    	display: inline
    	}

    many thanks to all who provided earlier input on this topic

    regards,

    Thread Starter TheElear

    (@peterpap)

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘alphabetize custom fields the_meta’ is closed to new replies.