WordPress.org

Ready to get started?Download WordPress

Forums

Search in admin help (2 posts)

  1. Simply Seath
    Member
    Posted 1 year ago #

    Hello Guys,

    I have custom post type with some custom columns from custom fields and taxonomies in admin area in post view.
    The thing is when I try to use the search whats on the right side above the custom columns it wont search the custom columns.

    Here is example of the code what I am using for the custom columns:

    add_filter( 'manage_edit-listings_columns', 'my_edit_listings_columns' ) ;
    
    function my_edit_listings_columns( $columns ) {
    
    	$columns = array(
    		'cb' => '<input type="checkbox" />',
    		'title' => __( 'Listing Title' ),
    		'address' => __( 'Address' ),
    		'price' => __( 'Price' ),
    		'author' => __( 'Agent' ),
    		'property_style' => __( 'Style' ),
    		'date' => __( 'Published' ),
    	);
    
    	return $columns;
    } // END function
    
    add_action( 'manage_listings_posts_custom_column', 'my_manage_listings_columns', 10, 2 );
    
    function my_manage_listings_columns( $column, $post_id ) {
    	global $post;
    
    	switch( $column ) {
    
    	// Address Column
    	case 'address' :
    		$address = get_post_meta( $post_id, 'address', true );
    		if ( empty( $address ) )
    			echo __( 'Unknown' );
    		else
    			printf( __( '%s ' ), $address );
    	break;
    
    	// Price Column
    	case 'price' :
    		$price = get_post_meta( $post_id, 'price', true );
    		if ( empty( $price ) )
    			echo __( 'Unknown' );
    		else
    			printf( __( '<span class="green"> $ %s</span>' ), $price );
    	break;
    
    	// Style ( Taxonomy ) Column
    	case 'property_style' :
    		$terms = get_the_terms( $post_id, 'property_style' );
    		if ( !empty( $terms ) ) {
    			$out = array();
    			foreach ( $terms as $term ) {
    				$out[] = sprintf( ' <a href="%s">%s</a>',
    					esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'property_style' => $term->slug ), 'edit.php' ) ),
    					esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'property_style', 'display' ) )
    				);
    			}
    			echo join( ' ', $out );
    		}
    			else {
    				_e( ' ' );
    			}
    	break;
    	}}

    And the sorting code:

    add_filter( 'manage_edit-listings_sortable_columns', 'devpress_listings_sortable_columns' );
    function devpress_listings_sortable_columns( $columns ) {
    	$columns['price'] = 'price';
    	return $columns;
    } // END function
    
    add_action( 'load-edit.php', 'devpress_edit_listings_load' );
    function devpress_edit_listings_load() {
    	add_filter( 'request', 'devpress_sort_price' );
    }
    function devpress_sort_price( $vars ) {
    	if ( isset( $vars['listings'] ) && 'price' == $vars['listings'] ) {
    		if ( isset( $vars['orderby'] ) && 'duration' == $vars['orderby'] ) {
    			$vars = array_merge(
    				$vars,
    				array(
    					'meta_key' => 'price',
    					'orderby' => 'meta_value_num'
    				)
    			);
    		}
    	}
    	return $vars;
    }

    I am using the same code on my older wordpress install and only difference is that now I am using Advance Custom Fields plugin for the custom fields. The search works fine on my older install.
    Can you guys please help me to figure out how to add the custom columns to the search. Or what I am doing wrong here.

    Thanks a lot.

  2. alexros
    Member
    Posted 1 year ago #

    Hello, i have the same problem, we need to search posts by a custom field in admin area. Till now we did not find any plugin or solution for this. Althought it should be really easy because we did it for the front-end. But we have a problem with a back end

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.