I created a custom post type with different input fields. For example first name and last name.
So I want this inputs as title in the post column-list. To do this I used the filter option:
add_filter( 'the_title', function( $title ) {
$title_firstname = esc_html( get_post_meta( get_the_ID(), 'first_name', true ) );
$last_name = esc_html( get_post_meta( get_the_ID(), 'last_name', true ) );
$title = $first_name . ' ' . $last_name;
return $title;
} );
Okay this works for me but there is one problem:
All titles form default-posts and pages are gone.
Whta can I do to change only my custom post titles?
Hope somebody can help me :)
Kind reagrds,
Jop
You can put in a conditional check to see what post type is being displayed to modify the title for only those custom posts.
add_filter( 'the_title', 'change_custom_post_title', 10, 2 );
function change_custom_post_title( $title, $id ) {
global $wp_query;
if( 'custom_post_type' !== get_post_type( $wp_query->post->ID ) )
return $title;
return 'My New custom post title';
}
Related
I would like to modify the get_author_posts_url function to get a custom taxonomy guest author slug that I have created.
Is there a way to change the following function to fetch a custom taxonomy link? I have used a filter to register the guest author as the post author by using a custom taxonomy, but I couldn't find a way to connect it to this get_author_posts_url function:
function get_author_posts_url( $author_id, $author_nicename = '' ) {
global $wp_rewrite;
$author_id = (int) $author_id;
$link = $wp_rewrite->get_author_permastruct();
if ( empty( $link ) ) {
$file = home_url( '/' );
$link = $file . '?author=' . $author_id;
} else {
if ( '' === $author_nicename ) {
$user = get_userdata( $author_id );
if ( ! empty( $user->user_nicename ) ) {
$author_nicename = $user->user_nicename;
}
}
$link = str_replace( '%author%', $author_nicename, $link );
$link = home_url( user_trailingslashit( $link ) );
}
Besides using the following filter, I also added the custom taxonomy (autoria) as the base slug instead of the /author slug. It's all working in the way that it is registering the custom author as the post author and also creating a custom taxonomy link in which I can access all posts by a certain custom author, but whenever I try to fetch the Author_URL it still shows the user that created the post (admin).
The filter I'm using is:
add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );
function guest_author_name( $name ) {
global $post;
$author = wp_get_post_terms( $post->ID, 'autoria', array( 'fields' => 'names' ) );
$author = implode( ', ', $author );
if ( $author )
$name = $author;
return $name;
}
Is there a way to connect this guest_author_name function to register the URL as well?
Thanks a lot, any help is appreciated
I like to get the value from the Aelia Currency Switcher plugin which is the '_order_total_base_currency' and make a simple USD conversion then display it on my custom field metabox. How do I fetch that value so I can use it for calculation, and then display?
Here is my code:
// Adding the metabox (on the right side)
add_action( 'add_meta_boxes', 'cdmb_add_meta_box');
function cdmb_add_meta_box() {
add_meta_box(
'woocommerce-order-my-custom',
__('USD Currency display'),
'cdmb_display_meta_box',
'shop_order',
'side',
'core'
);
}
// The metabox content
function cdmb_display_meta_box() {
// Get
$total_usd = (get_post_meta( $post->ID, '_order_total_base_currency', true )) / 0.75;
$total_usd .= get_post_meta( $post->ID, '_order_total_base_currency', true );
echo '<p>' . $total_usd . '</p>';
}
// Save/Update the meta data
add_action( 'save_post', 'cdmb_save_meta_box_data' );
function cdmb_save_meta_box_data( $post_id ) {
// Only for shop order
if ( 'shop_order' != $_POST[ 'post_type' ] )
return $post_id;
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
## SETTING AND UPDATING DATA ##
update_post_meta( $post_id, 'total-usd', sanitize_text_field( $_POST[ 'total-usd' ] ) );
}
?>
After spending time with it, I found the answer. I am now able to fetch the value for '_order_total_base_currency' from the Aelia Currency Switcher plugin.
It needs the global $post; before the variable $total_usd.
The code should be this:
function cdmb_display_meta_box() {
// Get
global $post;
$total_usd = get_post_meta( $post->ID, '_order_total_base_currency', true );
echo '<p>' . $total_usd . '</p>';
I have added a custom text field to woocommerce and also displayed it in the frontend of WC Vendor pro. So vendors can add a youtube or vimeo link to embed their movies.
However for some reason I can't get it to save and display in the product page on the front end.
The code I have so far in functions.php:
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => 'video_url',
'label' => __( 'Your product video link (youtube/vimeo)', 'woocommerce' ),
'placeholder' => 'https://',
'desc_tip' => 'true',
'description' => __( 'Copy the Youtube or Vimeo link here', 'woocommerce' )
)
);
echo '</div>';
}
function woo_add_custom_general_fields_save( $post_id ){
// Text Field
$woocommerce_text_field = $_POST['video_url'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, 'video_url', esc_attr( $woocommerce_text_field ) );
}
And to display the field in the vendor section:
<div class="all-100">
<!-- Media uploader -->
<div class="wcv-product-media">
<?php BuddyBoss_BM_Templates::product_media_uploader( $object_id ); ?>
<?php woo_add_custom_general_fields( $object_id ); ?>
</div>
</div>
So now vendors can enter the field. However saving nor displaying it won't work. I added the following to the product page:
echo $youtubevideo_code = wp_oembed_get( get_field('video_url') );
// tried this one as well:
echo get_post_meta( $post->ID, 'video_url', true );
Thanks any help would be greatly appreciated!
Assuming you use acf to use get_field, i think you miss the second parameter $post_id, the function need to know the post id of the field you want to get.
echo $youtubevideo_code = wp_oembed_get( get_field('video_url') );
You must right it like this
global $post; // I don't know where is your script (in the loop ? in a function ?)
$youtubevideo_code = wp_oembed_get( get_post_meta($post->ID, 'video_url', true )); // can be get_field('video_url, $post->ID);
echo $youtubevideo_code;
I am using the Advanced custom fields plugin and Custom post type UI to give my users some extra functionality. The problem I have is that I have setup a users information menu and in the list view all new posts appear as Auto Draft. Is there anyway I can make the field slug company name act as the post title for the list view?.
I tried the below code but it is not updating the company name as post title and in the custom post page it display message like "You are currently editing the page that shows your latest posts."
My Sample code:
add_filter('title_save_pre', 'save_title');
function save_title() {
if ($_POST['post_type'] == 'users') : // my custom post type name
$new_title = $_POST['company_name']; // my custom field name
$my_post_title = $new_title;
endif;
return $my_post_title;
}
Use name="post_title" in your input.
<input type="text" name="post_title" id="meta-text" class="form-control" value="">
this should work:
add_action( 'acf/save_post', 'save_post_handler' , 20 );
function save_post_handler( $post_id ) {
if ( get_post_type( $post_id ) == 'users' ) {
$title = get_field( 'company_name', $post_id );
$data['post_title'] = $title;
$data['post_name'] = sanitize_title( $title );
wp_update_post( $data );
}
}
disclaimer: I literally just read the php website top to bottom yesterday, but I read a few posts about trying to do this and assembled this solution that is working for me. I have a custom post type called artists, I combine the artist acf field of first_name and last_name and set that as the title. For your example you could remove the parts that add last name.
// Auto-populate artist post type title with ACF first name last name.
function nd_update_postdata( $value, $post_id, $field ) {
// If this isn't an 'artists' post type, don't update it.
if ( get_post_type( $post_id ) == 'artists' ) {
$first_name = get_field('first_name', $post_id);
$last_name = get_field('last_name', $post_id);
$title = $first_name . ' ' . $last_name;
$slug = sanitize_title( $title );
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'artists',
'post_name' => $slug
);
wp_update_post( $postdata, true );
return $value;
}
}
add_filter('acf/update_value/name=first_name', 'nd_update_postdata',
10, 3);
add_filter('acf/update_value/name=last_name', 'nd_update_postdata', 10,
3);
I added a custom field to a single product page for woocommerce in order to show ISBN number for the books I sell. I found a nice guide and managed to add everything as I want. However when I empty the custom field for ISBN it won't go empty on the site.
I have the following code in the functions.php
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Custom fields will be created here...
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_ISBN_field',
'label' => __( 'ISBN', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'ISBN.', 'woocommerce' )
)
);
function woo_add_custom_general_fields_save( $post_id ){
// Customer text ISBN Field
$woocommerce_text_field = $_POST['_ISBN_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_ISBN_field', esc_attr( $woocommerce_text_field ) );
}
Then in the short-description.php I made it so that it shows on the product page. However it still displays the name ISBN10: if it's an empty field.
<?php
// Display Custom Field Value
if (!((get_post_meta($post->ID, '_ISBN_field', true))==”)) {
//Not empty
echo '<b>ISBN10: </b>',get_post_meta( $post->ID, '_ISBN_field' , true);
}
?>
So the two problems are I can't edit the product to contain an empty custom field. And if the field is empty (only possible when field hasn't been previously contained data) it still displays the field name.
Thanks in advance.
Your save function should be like
function woo_add_custom_general_fields_save( $post_id ){
// Customer text ISBN Field
$woocommerce_text_field = $_POST['_ISBN_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_ISBN_field', esc_attr( $woocommerce_text_field ) );
else
update_post_meta( $post_id, '_ISBN_field', '' );
}
If !empty( $woocommerce_text_field ) returns true only if $_POST['_ISBN_field'] has some value so the post meta is not updated if $_POST['_ISBN_field'] is empty
what does:
var_dump( get_post_meta( $post->ID, '_ISBN_field' , true) );
return?
i guess the problem ist that the field still contains some value even it's empty..
check that var_dump and than adjust your if statement
and i guess the statement should be like:
if ( get_post_meta( $post->ID, '_ISBN_field', true ) != '' ) {
Try this:
<?php
// Display Custom Field Value
$ISBN_field = get_post_meta($post->ID, '_ISBN_field', true);
if( !empty( $ISBN_field ) ){
echo '<b>ISBN10: </b>'.$ISBN_field;
} ?>
Regards