• Resolved simonemeconi

    (@simonemeconi)


    Hello everyone,
    I need help to resolve this problem. I have created 3 more admin columns with code. This Admin columns shows 3 custom fields from metabox.
    I also have limited the “Author” role to see only their own posts.

    The problem is that if I limit Author, them can’t see the admin columns content.

    // Script for limit Author
    
    add_filter( 'pre_get_posts', function( $query ) {
    	global $pagenow;
    	global $user_ID;
    
    	if( 'edit.php' != $pagenow || ! $query->is_admin )
    		return $query;
    
    	if( ! current_user_can( 'administrator' ) ) {
    		$query->set('author', get_current_user_id() );
    	}
    
    	return $query;
    });

    And behind there are the screenshots to see the difference between the script is active or not.

    Do you have any idea or solution?

    Thanks a lot.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Please share the you code use to place content in your custom columns. If the code is more than a couple dozen lines, you should post outside of these forums and just provide the link here. A couple places where you can post your code are pastebin.com or gist.github.com.

    Thread Starter simonemeconi

    (@simonemeconi)

    <?php 
    
    add_filter( ‘manage_animali_posts_columns’, function( $columns ) {
    	$columns[ ‘anteprima’ ] = __( ‘Anteprima’ );
    	$columns[ ‘razza_animale’ ] = __( ‘Razza Animale’ );
    	$columns[ ‘data_nascita’ ] = __( ‘Data di Nascita’ );
    	$columns[ ‘data_ingresso’ ] = __( ‘Data di Ingresso’ );
        return $columns;
    } );
    add_action( ‘manage_animali_posts_custom_column’, function( $column_name, $post_id ) {
    	$razza_animale = rwmb_get_value( ‘razza_animale’, $post_id );
    	$image = rwmb_get_value( ‘galleria_immagini_animale’, [ ‘size’ => ‘thumbnail’ ], $post_id )
    	$data_nascita = rwmb_get_value( ‘data_nascita_animale’, $post_id );
    	$data_ingresso = rwmb_get_value( ‘data_ingresso_animale’, $post_id ); 
    	if( ‘razza_animale’ == $column_name ) {
    	    echo $razza_animale;
    	}
    	if ( ‘anteprima’ == $column_name ) {
    		echo ‘<img src=“’ . $image[ ‘url’ ] . ‘”>’;
    	}
    	if( ‘data_nascita’ == $column_name ) {
    	    echo $data_nascita;
    	}
    	if( ‘data_ingresso’ == $column_name ) {
    	    echo $data_ingresso;
    	}
    }, 10, 2 );
    
    
    Moderator bcworkz

    (@bcworkz)

    I don’t see how altering the query results by author would affect custom columns in the list table. However, there are errors in your code that need to be addressed.

    If I’m not mistaken, I think the first add_filter() call should be
    add_filter( ‘manage_edit-animali_columns’, function( $columns ) {
    I’m not sure how you’d get any custom columns under any query with what you have: manage_animali_posts_columns

    Many of the quotes are the “curly” kind — , which are not seen as quotes in PHP. They need to be changed to the “straight” kind — '.

    This line was missing the terminal ;:
    $image = rwmb_get_value( ‘galleria_immagini_animale’, [ ‘size’ => ‘thumbnail’ ], $post_id );

    Some of these may be due to copy/paste issues into the forums. The original source may be fine. I did test your code with the above corrections, after defining rwmb_get_value() to return “test” and changing “animale” to an existing CPT of mine. I saw 4 custom columns of “test” for all of my posts as an author user.

    Thread Starter simonemeconi

    (@simonemeconi)

    Hello, my code is working because on an administrator interface I can see all. Maybe, as you said, is a problem on copy/past here in the forum.

    For the moment I resolved the issue by delete all this custom fields from the dashboard Author and put only the featured image. And like this it works.

    Thanks for the time you spent for me. Maybe I will check better in future.

    Thanks,
    Bye

    Moderator bcworkz

    (@bcworkz)

    If you’re happy I’m happy 🙂

    BTW, another possible cause would be what rwmb_get_value() is doing with author users. If its return value is empty for any reason custom columns in the list table would appear to not work.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Admin column not display content correctly’ is closed to new replies.