SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Sorting by a custom field in WordPress - ACF plugin

    I have successfully added a custom column to the WordPress back-end, but can’t get sorting to register.

    The page below was helpful.
    http://www.elliotcondon.com/advanced...ustom-columns/

    The ‘type’ field shows on the ‘press’ post-type but without sorting. No error is returned.

    Here is my code below:

    Code PHP:
    // ADD NEW COLUMN
    function ST4_columns_head($defaults) {
    	$defaults['type'] = 'Type';
    	$defaults['featured_image'] = 'Featured Image';
     
    	return $defaults;
    }
     
    // SHOW THE FEATURED IMAGE
    function ST4_columns_content($column_name, $post_ID) {
    	if ($column_name == 'featured_image') {
    		echo get_the_post_thumbnail( $post_ID, 'tiny', $attr );
    	}
     
    	if ($column_name == 'type') {
    		echo get_field( "type", $post_ID );
    	}
    }
     
    add_filter('manage_press_posts_columns', 'ST4_columns_head');
    add_action('manage_press_posts_custom_column', 'ST4_columns_content', 10, 2);
     
    //make the new column sortable
    function my_press_sortable_columns( $columns ) {
    	$columns['type'] = 'type';
    	return $columns;
    }
    add_filter( 'manage_press_sortable_columns', 'my_press_sortable_columns' );

    I know there are plugins that can do this, but I avoid plugins that can be replaced with a few lines of code.

    Thank you for your insights.
    E
    Last edited by eruna; Dec 3, 2013 at 08:43. Reason: grammar error

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,603
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Try changing this:

    Code:
    add_action('manage_press_posts_custom_column', 'ST4_columns_content', 10, 2);
    to this:

    Code:
    add_filter('manage_press_posts_custom_column', 'ST4_columns_content');
    And change your sortable_columns function to this:

    PHP Code:
    function my_press_sortable_columns($columns) {
        
    $custom = array(
                
    'type'         => 'type'
        
    );
        return 
    wp_parse_args($custom$columns);

    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •