I am trying to create a new column for custom post types that reveals the post ID in the Wordpress DB. The custom posts are Recipes from the WP Ultimate Recipe Plugin. The code below works if I change the hook for just posts, but even with the Plugin Developers advice on which hooks of his to use, it won't work...
add_filter( 'manage_recipe_posts_columns', 'revealid_add_id_column', 5 );
add_action( 'manage_recipe_posts_custom_column', 'revealid_id_column_content', 5, 2 );
function revealid_add_id_column( $columns ) {
$columns['revealid_id'] = 'ID';
return $columns;
}
function revealid_id_column_content( $column, $id ) {
if( 'revealid_id' == $column ) {
echo $id;
}
}
Any idea on how to get this to work?
Try this code,
function add_cpt_columns( $columns ) {
$column_meta = array( 'your-column-slug' => 'your column name' );
//column rearrange
$columns = array_slice( $columns, 0, 3, true ) + $column_meta + array_slice( $columns, 3, null, true );
return $columns;
}
function custom_custom_cpt_column( $column, $post_id ) {
switch ( $column ) {
case 'your-column-slug' :
// your process
break;
}
}
add_filter( 'manage_edit-custom-post-type_columns', 'add_cpt_columns' );
add_action( 'manage_custom-post-type_posts_custom_column' , 'custom_custom_cpt_column', 10, 2 );
Related
This is what I have tried to export/import the post_status with the default Woocommerce CSV importer/exporter.
I have managed to get the post_status field to show up mapped in the importer/exporter.
It also exports the column to csv. However there is no data.
How could I get the post_status to export and import correctly?
// ADD CUSTOM IMPORT COLUMN TO CSV IMPORTER
add_filter( 'woocommerce_csv_product_import_mapping_options', 'add_column_to_importer' );
function add_column_to_importer ( $options ) {
$options['post_status'] = 'post_status';
return $options;
}
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'add_column_to_mapping_screen' );
function add_column_to_mapping_screen ( $columns ) {
$columns['post_status'] = 'post_status';
return $columns;
}
add_filter( 'woocommerce_product_import_pre_insert_product_object', 'process_import', 10, 2 );
function process_import( $object, $data ) {
if (!empty($data['post_status'])) {
$object->post_status($data['post_status']);
}
return $object;
}
// ADD CUSTOM EXPORT COLUMN TO CSV EXPORTER
add_filter( 'woocommerce_product_export_column_names', 'add_export_column' );
add_filter( 'woocommerce_product_export_product_default_columns', 'add_export_column' );
function add_export_column( $columns ) {
$columns['post_status'] = 'post_status';
return $columns;
}
add_filter( 'woocommerce_product_export_product_column_custom_column', 'add_export_data', 10, 2 );
function add_export_data( $value, $product ) {
$post_status = get_post_status();
return $post_status;
}
You have an issue inside your hooks. Check the last hook, for example. You need to replace ...._custom_column with your actual column which is in your case post_status:
add_filter( 'woocommerce_product_export_product_column_post_status', 'add_export_data', 10, 2 );
function add_export_data( $value, $product ) {
return $product->get_status();
}
add_filter( 'woocommerce_product_import_pre_insert_product_object', 'process_import', 10, 2 );
function process_import( $product, $data ) {
if ( ! empty( $data['post_status'] ) ) {
$product->set_status( $data['post_status'] );
}
return $product;
}
This hook is used to add some data to your column and because the hook is incorrect, no data is visible.
I've added a couple of custom field columns to our Woocommerce orders list in the admin of WordPress using the methods below, but the sort is not working....
add_filter( 'manage_edit-shop_order_columns', 'my_wc_columns' );
function my_wc_columns($columns){
$new_columns = (is_array($columns)) ? $columns : array();
unset( $new_columns['order_actions'] );
$new_columns['program_id'] = 'Program';
$new_columns['constituent_id'] = 'Constituent ID';
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'my_wc_column_values', 2 );
function my_wc_column_values($column){
global $post;
if ( $column == 'program_id' ) {
$program = get_post_meta( $post->ID, '_program_id', true );
$program_title = get_the_title($program);
$column_val = (isset($program) && $program>0 ? $program_title : 'All');
echo '<span>' . my_programs_get_name( $column_val ) . ' (' . $program . ')</span>';
}
if ( $column == 'constituent_id' ) {
$consid = get_post_meta( $post->ID, 'constituent_id', true );
$column_val = (isset($consid) && $consid != "") ? $consid : "";
echo '<span>' . $column_val . '</span>';
}
}
// Make column sortable
add_filter( "manage_edit-shop_order_sortable_columns", 'my_wc_column_sort' );
function my_wc_column_sort( $columns ) {
$custom = array(
'program_id' => '_program_id',
'constituent_id' => 'constituent_id',
);
return wp_parse_args( $custom, $columns );
}
I expected to have an issue perhaps with the program name, since it is an id that needs to be translated via a custom function to a name, but neither column is sorting properly. The records change order after clicking their column titles, but I cannot tell how the sort is being done. The program is not sorting on name or ID and both are seem random but consistent. Keep in mind both fields are custom fields that may or may not have a value defined. How can I make this sortable?
Here's a good tutorial on custom sortable columns. After you register the column, you need to handle the actual sorting. Sadly, that part doesn't happen automagically. Untested, but adapted from the above tutorial:
add_action( 'pre_get_posts', 'manage_wp_posts_be_qe_pre_get_posts', 1 );
function manage_wp_posts_be_qe_pre_get_posts( $query ) {
/**
* We only want our code to run in the main WP query
* AND if an orderby query variable is designated.
*/
if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {
switch( $orderby ) {
// If we're ordering by 'program_id'
case 'program_id':
// set our query's meta_key, which is used for custom fields
$query->set( 'meta_key', '_program_id' );
/**
* Tell the query to order by our custom field/meta_key's
* value
*
* If your meta value are numbers, change 'meta_value'
* to 'meta_value_num'.
*/
$query->set( 'orderby', 'meta_value' );
break;
}
}
}
We have two plugins in a WordPress website
1) Plugin A has the code below
add_filter( 'manage_edit-book_columns', 'set_custom_edit_book_columns' );
add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
function set_custom_edit_book_columns($columns) {
unset( $columns['author'] );
$columns['book_author'] = __( 'Author', 'your_text_domain' );
$columns['publisher'] = __( 'Publisher', 'your_text_domain' );
return $columns;
}
function custom_book_column( $column, $post_id ) {
switch ( $column ) {
case 'book_author' :
$terms = get_the_term_list( $post_id , 'book_author' , '' , ',' , '' );
if ( is_string( $terms ) )
echo $terms;
else
_e( 'Unable to get author(s)', 'your_text_domain' );
break;
case 'publisher' :
echo get_post_meta( $post_id , 'publisher' , true );
break;
}
}
2) Now, I want to add one more column in this grid by adding code in plugin B , I don't want to edit plugin A to make fixes
3) Is it possible that we copy the code in plugin B? When I did so then there has double data in all columns. From plugin A and plugin B.
You simply have to add different data, at least different column IDs.
<?php
/**
* Plugin Name: Plugin B
*/
add_filter( 'manage_edit-book_columns', 'set_custom_edit_book_columns' );
add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
function set_custom_edit_book_columns($columns) {
$columns['new_column'] = __( 'Brand new column' ); // <-- Column ID
return $columns;
}
function custom_book_column( $column, $post_id ) {
switch ( $column ) {
case 'new_column' : // <-- Column ID
echo "Anything you want";
break;
}
}
I'v searched around a good bit and can't find anything but a plugin, which breaks WP.
I'm using WooCommerce, so a custom post type is involved.
In the Products list page, I'd like to add a new Column, that displays the Slug of each product url next to each product.
I found a way to show the ID really easy, but not the slug.
Thank you
First you want to add a slug column after the product name.
// Add product slug column after product name
function add_product_slug_column_heading( $columns ) {
$slug_column = array(
'product_slug' => __( 'Slug' )
);
$columns = array_slice( $columns, 0, 3, true ) + $slug_column + array_slice( $columns, 3, count( $columns ) - 1, true );
return $columns;
}
Then use the manage_product_posts_custom_column filter to display the slug value.
// Display product slug
function add_product_slug_column_value( $column_name, $id ) {
if ( 'product_slug' == $column_name ) {
echo get_post_field( 'post_name', $id, 'raw' );
}
}
add_action( "manage_product_posts_custom_column", 'add_product_slug_column_value', 10, 2 );
No coding solution
If you're just looking for a quick solution without coding you could use admin columns, completely free. It allows you to add the same slug column with just a few clicks.
Here is the exact working code:
// Add product slug column after product name
function add_product_slug_column_heading( $columns ) {
$slug_column = array(
'product_slug' => __( 'Slug' )
);
$columns = array_slice( $columns, 0, 3, true ) + $slug_column + array_slice( $columns, 3, count( $columns ) - 1, true );
return $columns;
}
add_filter('manage_edit-product_columns','add_product_slug_column_heading');
// Display product slug
function add_product_slug_column_value( $column_name, $id ) {
if ( 'product_slug' == $column_name ) {
echo get_post_field( 'post_name', $id, 'raw' );
}
}
add_action( "manage_product_posts_custom_column", 'add_product_slug_column_value', 10, 2 );
I want to use something like:
http://example.com/%category_id%/%postname%/
for the permalink structure.
For example, if a post has a category with an ID of 3, then the URL for the post will be
http://example.com/3/post-name/
Does anyone know how this can be done? I don't mind modifying WordPress core.
This code adds the %category_id% rewrite tag, and filters post permalinks to replace them with the actual category ID (lowest if there are multiple categories). You can place this in a plugin or in your theme file.
add_action( 'init', 'so6159452_init' );
function so6159452_init()
{
add_rewrite_tag( '%category_id%', '([0-9]+)' );
}
add_filter( 'post_link', 'so6159452_post_link', 10, 2 );
function so6159452_post_link( $permalink, $post )
{
if ( false !== strpos( $permalink, '%category_id%' ) ) {
$cats = get_the_category( $post->ID );
if ( $cats ) {
usort( $cats, '_usort_terms_by_ID' ); // order by ID
$category_id = $cats[0]->cat_ID;
} else {
// Error: no category assigned to this post
// Just use a dummy variable
$category_id = '0';
}
$permalink = str_replace( '%category_id%', $category_id, $permalink );
}
return $permalink;
}