I have a taxonomy named talent_cat which is the category of said post, has 4 values: modelos, atores, musica, djs and inside each individual talent_cat there are these post meta keys:talent_id_1495127471815 for modelostalent_id_1495127471816 for atorestalent_id_1495127471817 for musica talent_id_1495127471818 for djs
How can I build a conditional to show the right post meta depending on the talent_cat of said post? So if the post belongs to talent_cat = modelos it returns the talent_id_1495127471815 and not the other values.
I tried this but unsuccessfully:
$talent_data = get_the_term_list( $post->ID, 'talent_cat');
switch( $talent_data ) {
case 'modelos':
echo get_post_meta(get_the_ID(), 'talent_id_1495127471815', true);
break;
}
Thanks.
The function get_the_term_list returns HTML but you need to get an array or object to check your statement. You need to use get_the_terms function instead. Your code should look like this:
global $post;
$talent_data = get_the_terms( $post->ID, 'talent_cat');
// check for errors or empty data
if ( is_wp_error( $talent_data ) || empty( $talent_data ) ) {
return;
}
foreach ( $talent_data as $term ) {
// you can check for name or slug or id
switch( $term->slug ) {
case 'modelos':
echo get_post_meta($post->ID, 'talent_id_1495127471815', true);
break;
}
}
Related
I've read other answers about assigning category based on post tags. But can this be done based on postmeta?
I'm assuming it can be and I've been trying to change the following snippet (quoted in another answer) to achieve this. But I've had no luck tweaking it to reference postmeta meta_key (delivery_option) and meta_value (pick-up, postal, post & parcel), to then auto assign a category (pick-up, postal or post & parcel).
In case it's relevant, the above postmeta key and value have been added by another plugin.
function auto_add_category ($product_id = 0) {
if (!$product_id) return;
// because we use save_post action, let's check post type here
$post_type = get_post_type($post_id);
if ( "product" != $post_type ) return;
$tag_categories = array (
'ring' => 'Jewellery'
'necklace' => 'Jewellery',
'dress' => 'Clothing',
);
// get_terms returns ALL terms, so we have to add object_ids param to get terms to a specific product
$product_tags = get_terms( array( 'taxonomy' => 'product_tag', 'object_ids' => $product_id ) );
foreach ($product_tags as $term) {
if ($tag_categories[$term->slug] ) {
$cat = get_term_by( 'name', $tag_categories[$term->slug], 'product_cat' );
$cat_id = $cat->term_id;
if ($cat_id) {
$result = wp_set_post_terms( $product_id, $cat_id, 'product_cat', true );
}
}
}
}
add_action('save_post','auto_add_category');
Disclosure: I'm building a WordPress website and learning as I go. This may be an obvious question, but be assured that its being asked after hours of research to try and answer myself (it's all good I've learnt other stuff while researching... just not the right stuff!). HUGE thanks in advance for any mastery insights.
This code when placed in your functions.php file will check the product's delivery option and then assign the corresponding category to the product. If any product categories for that product already exist it will append them to the list. The product category would need to exist in the first place and if it does then it assigns that category with the same slug as the delivery option. I use the hook save_post_product so that it fires only on updating products.
add_action('save_post_product', 'update_product_category', 20, 3);
function update_product_category( $post_id, $post, $update ) {
$product = wc_get_product( $post_id );
$delivery_methods = array( 'pick-up', 'postal', 'post', 'parcel' );
$delivery_option = get_post_meta($post_id, 'delivery_option', true);
if( ! empty( $delivery_option ) ) {
$product_cats = $product->get_category_ids();
foreach( $delivery_methods as $delivery_method) {
if( $delivery_option === $delivery_method ) {
$pickup_cat_id = get_term_by('slug', $delivery_method, 'product_cat')->term_id;
if( $pickup_cat_id && ! in_array( $pickup_cat_id, $product_cats) ) {
$product_cats[] = $pickup_cat_id;
$product->set_category_ids($product_cats);
$product->save();
}
}
}
}
}
the following code in my function.php can almost do the job nice and clean to all single pages. the problem is I want it to be filtered for a specific category ID:
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
if ( $GLOBALS['post']->ID == get_the_ID() ) {
$content .= 'Your new content here';
}
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
You don't need to use get_the_category and examine the results - Wordpress already has a function to check if your post is in a category: has_category. The advantage of this over get_the_category is that
you don't need the code to loop through all results to comparing the id (this is the part that is wrong in the other answer - it will only work if your post has one single category)
You can use not just the ID, but also the slug or name
You can check for multiple categories
The code you need is very simple, you just need to change one line!
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
/* pass an array with the IDs/names/slugs of the categories to check for, e.g. */
if ( has_category( array(12) ) )
$content .= 'Your new content here';
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
UPDATE:
Even though it wasn't in your question, if you actually want to include the content on all posts as well, you just need to do this:
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
$content .= 'Your new content here';
if ( has_category( array(12) ) )
$content .= '<br>Your new content here';
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
Modified your code idealy this condition is never making any sense because it always getting the id. $GLOBALS['post']->ID == get_the_ID()
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
if ( $GLOBALS['post']->ID == get_the_ID() ) {
$content .= 'Your new content here';
}
//getting the current post
global $post;
$category_detail=get_the_category( $post->ID );
if(!empty($category_detail))
{
$catId = $category_detail[0]->term_id;
// instead of 2 put your category id
if($catId == 2 && $catId != 0 )
{
$content .= 'Your new content here';
}
}
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
Note : above code is for only default post not for custom taxonomy for custom taxonomy
How to check if post have meta data and display it by function?
I have post type Portfolio and meta fields (_wi_next-script-part and _wi_prev-script-part)
function wi_next_prev_part_links($content) {
global $wp_query;
$postid = $wp_query->post->ID;
if ( is_singular( 'portfolio' ) ) {
if ( get_post_meta( $postid, '_wi_prev-script-part', true ) ) {
$prev_script_part = get_post_meta( $postid, '_wi_prev-script-part', true );
$content .= '← Prev Part Link';;
}
if ( get_post_meta( $postid, '_wi_next-script-part', true ) ) {
$next_script_part = get_post_meta( $postid, '_wi_next-script-part', true );
$content .= 'Next Part Link →';
}
}
return $content;
}
add_filter ('the_content', 'wi_next_prev_part_links', 0);
There are two ways of doing this. One is to check the value of the meta key and one is to check if the meta key exists.
Checking the value
As explained in the get_post_meta documentation the function will return an empty string or an empty array if the meta key your tried accessing does not exists. Since both an empty string and an empty array are falsy you can use a simple if statement to check whether a meta key has valid meta data or not as such:
if ( get_post_meta( $postid, '_wi_prev-script-part', true ) )
{
//Do something
}
Checking for the key
However, in some instances an empty string is considered to be a valid value. If your code regards empty string as a desirable value you will need to check the for the existence of the meta key itself. We can do that with the get_post_custom_keys function:
if( in_array( '_wi_prev-script-part', get_post_custom_keys($postid) ) )
{
//Do something
}
I have created a custom post type songs which has all the songs and now I want to sort it according to alphabets, which I have done using
function alphaindex_save_alpha( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
$slug = 'songs';
$letter = '';
if ( isset( $_POST['post_type'] ) && ( $slug != $_POST['post_type'] ) )
return;
if ( !current_user_can( 'edit_post', $post_id ) )
return;
$taxonomy = 'alpha';
if ( isset( $_POST['post_type'] ) ) {
$title = strtolower( $_POST['post_title'] );
// Get the first letter of the title
$letter = substr( $title, 0, 1 );
}
//set term as first letter of post title, lower case
wp_set_post_terms( $post_id, $letter, $taxonomy );
}
add_action( 'save_post', 'alphaindex_save_alpha' );
now this creates a taxonomy alpha which maps every songs based on the first alphabet for eg. Adele is mapped to A and so on.
I created this alphabet index after I had already put 2000 songs in my word press website and now for the songs to get mapped to the taxonomy I have to individually go to every post page and update the post and only then it appears.
But, doing this is a very tedious task and want a code that can do it automatically from the functions.php file.
I would recommend using posts_where filter instead of taxonomy. You do not need to add any extra data to your database to accomplish this.
add_filter('posts_where', 'starts_with_filter');
function starts_with_filter($sql){
if ( ! $query->is_main_query() || ! isset(get_query_var('starts_with')) {
return $query;
}
global $wpdb;
$startswith = substr ( get_query_var( 'starts_with' ), 0, 1);
$sql .= $wpdb->prepare( " AND $wpdb->posts.post_title LIKE %s ", $startswith.'%' );
return $sql;
}
I found this function to display the terms attached to a post but i can't manage to find a way to exclude specific ID's of some category terms i don't want to display in the list.
Could someone give me a clue were to start? I looked up all the functions used in this function but can't seem to find arguments to exclude id's.
Thanks in advance!
// get taxonomies terms links
function custom_taxonomies_terms_links() {
global $post, $post_id;
// get post by post id
$post = &get_post($post->ID);
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies($post_type);
$out = "<ul>";
foreach ($taxonomies as $taxonomy) {
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy );
if ( !empty( $terms ) ) {
foreach ( $terms as $term )
$out .= '<li>'.$term->name.'</li> ';
}
}
$out .= "</ul>";
return $out;
}
Add another conditional if statement within your second foreach() to check whether or not the $term should be ignored. For example:
// An array of IDs to ignore/exclude
$excluded_ids = array( 1, 2, 3, 4);
foreach ( $terms as $term ) {
// Only proceed if the term_id is NOT in the $excluded_ids array
if ( !in_array( $term->term_id, $excluded_ids ) ) {
$out .= '<li>'.$term->name.'</li> ';
}
}