Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Robert

Columns on Media list – fields of attached Posts

Media items can be attached to Posts.

How could I add to the Media Library list some fields from any attached Posts?

One is there by default – the post title.

How could I add the post date? I’d be interested in ordering and filtering the Media Library list by the date and other fields, since this would make it easy to bulk-organise eg images from a particular date.

Never made an extension before. Unclear how complex this would be. Any existing stuff tools?

1 year, 10 months ago
Robert

I have been following the custom-column instructions – https://docs.admincolumns.com/article/21-how-to-create-my-own-column

I’ve downloaded the template, replaced placeholder values and enabled the plugin.

“That’s it, your column type plugin is ready for testing!”

But the new column does not appear in any ACP dropdowns.

Even if I leave the placeholder values, I don’t see COLUMN_LABEL anywhere.

What am I doing wrong?

1 year, 10 months ago
Robert

Oh, I see… the plugin file contains various scenarios, preset to “page”.

Okay, I’ve swapped all the names correctly. I can get the following to working in the Media Library list to display the first “topic” term of any post connected to the image…

/**
	 * Returns the display value for the column.
	 *
	 * @param int $id ID
	 *
	 * @return string Value
	 */
	public function get_value( $post_id ) {

		$connected_post_id = get_post($post_id)->post_parent;
		
		$post_series_term = get_the_terms( $connected_post_id, 'topic' );
		if ( $post_series_term ) {
			$value = '<a href="' . esc_url( get_permalink( $post_id ) ) . '">' . $post_series_term[0]->name . '</a>';
		} else {
			$value = '-';
		}

		return $value;
	}

But:

a) The “attachment” type for Example #2, media, wouldn’t show this. I’ve had to leave it on the Example #3 code for all post types. How can I sort this?

b) I’m now needing to build out the sorting and filtering. What should I be thinking in those files?

1 year, 10 months ago
Robert

I’ve seemingly got classes/Free/my_column.php functioning well.

But I’m confused about how to enabling sorting, filtering, editing etc. The column header is not active for clicking
and my column doesn’t appear in filters.

What more is necessary in classes/sorting/my_column.php… ?

			// Start editing here.

			// Put all the column logic here to retrieve the value you need
			// For example: $value = get_post_meta( $id, '_my_custom_field_example', true );

			$value = $this->column->get_raw_value( $id );

			// Stop editing.

It’s not clear what is meant by “logic”. Why isn’t $value simply taken from get_raw_value()?

Code below…

I also don’t understand what, if anything, I should be doing with classes/Pro/my_column.php

<?php

namespace mynamespace\Column\Free;

class my_column extends \AC\Column {

	public function __construct() {

		// Identifier, pick an unique name. Single word, no spaces. Underscores allowed.
		$this->set_type( 'column-my_column' );

		// Default column label.
		$this->set_label( __( 'My Column', 'ac-my_column' ) );
	}

	/**
	 * Returns the display value for the column.
	 *
	 * @param int $id ID
	 *
	 * @return string Value
	 */
	public function get_value( $post_id ) {

		// get raw value
		$value = $this->get_raw_value( $post_id );

		if ($value) {
			// optionally you can change the display of the value. In this example we added a post link.
			$value = '<a href="' . esc_url( get_permalink( $post_id ) ) . '">' . $value . '</a>';
		} else {
			$value = '–';
		}

		return $value;
	}

	/**
	 * Get the raw, underlying value for the column
	 * Not suitable for direct display, use get_value() for that
	 * This value will be used by 'inline-edit' and get_value().
	 *
	 * @param int $id ID
	 *
	 * @return mixed Value
	 */
	public function get_raw_value( $post_id ) {

		$connected_post_id = get_post($post_id)->post_parent;
		
		// Get all terms of post attached to this image
		$post_term = get_the_terms( $connected_post_id, 'topic' );

		// If there are any terms
		if ( $post_term ) {
			$value = $post_term[0]->name;
			return $value;
		}

		
	}

	/**
	 * (Optional) Create extra settings for you column. These are visible when editing a column. You can remove this function is you do not use it!
	 * Write your own settings or use any of the standard available settings.
	 */
	protected function register_settings() {

		// NOTE! When you use any of these settings, you should remove the get_value() method from this column, because the value will be rendered by the AC_Settings_Column_{$type} classes.

		// Display an image preview size settings screen
		// $this->add_setting( new \AC\Settings\Column\Image( $this ) );

		// Display an excerpt length input field in words
		// $this->add_setting( new \AC\Settings\Column\WordLimit( $this ) );

		// Display an excerpt length input field in characters
		// $this->add_setting( new \AC\Settings\Column\CharacterLimit( $this ) );

		// Display a date format settings input field
		// $this->add_setting( new \AC\Settings\Column\Date( $this ) );

		// Display before and after input fields
		// $this->add_setting( new \AC\Settings\Column\BeforeAfter( $this ) );

		// Displays a dropdown menu with user display formats
		// $this->add_setting( new \AC\Settings\Column\User( $this ) );

		// Displays a dropdown menu with post display formats
		// $this->add_setting( new \AC\Settings\Column\Post( $this ) );
	}

	/**
	 * (Optional) Is valid. You can remove this function is you do not use it!
	 * This determines whether the column should be available. If you want to disable this column
	 * for a particular post type you can set this to false.
	 * @return bool True/False Default should be 'true'.
	 */
	public function is_valid() {

		// Example: if the post type does not support thumbnails then return false
		// if ( ! post_type_supports( $this->get_post_type(), 'thumbnail' ) ) {
		//    return false;
		// }

		return true;
	}

	/*
	 * (Optional) Enqueue CSS + JavaScript on the admin listings screen. You can remove this function is you do not use it!
	 *
	 * This action is called in the admin_head action on the listings screen where your column values are displayed.
	 * Use this action to add CSS + JavaScript
	 */
	public function scripts() {

		wp_enqueue_script( 'ac-my_column', plugin_dir_url( __FILE__ ) . "../../../js/column.js" );
		wp_enqueue_style( 'ac-my_column', plugin_dir_url( __FILE__ ) . "../../../css/column.css" );
	}

}
1 year, 10 months ago
Robert

Well, I note your docs: “Features like filtering, sorting and inline edit are only available for the pro version of Admin Columns Pro. The /classes/Column/Pro/COLUMN_NAME.php file contains an implementation on how to make the column sortable and editable. You’ll need to implement an Editing and Sorting interface in your column class and create the associated models. This will allow you to enable sorting or editing in your column settings.”
https://docs.admincolumns.com/article/21-how-to-create-my-own-column

But I don’t quite see how to put this all together.

1 year, 10 months ago
Stefan van den Dungen Gronovius
Developer

Hi Robert,

Sorry for the late response.
In your case, it will be best to only create a pro column and not a free and pro column.
The sorting example in the toolkit is not the optimal way to perform sorting logic.
The example works based on the value, so in order to sort it must retrieve the value for every record in order to sort. In our own Sorting models, we perform sorting based on SQL queries, which is much faster that the provided way but also more complicated to write.

I can help you a bit further, but let’s do that by email instead of posting everything here.
Contact me at support@admincolumns.com

1 year, 10 months ago

You must be logged in to reply to this topic.