WordPress Custom Taxonomy Term Meta Value - php

I have created a Custom Taxonomy for Event Categories.
I have added a color picker form field in the category so all categories added can have a color associated with it.
I have the saved data in my wp_termeta table in the database:
meta_id = 1
term_id = 2
meta_key = _category_color
meta_value = 8224e3 (hex color)
I am displaying the events in fullcalendar and I am trying to grab the color of the event taxonomy and use it for the event background color in the calendar.
I am using a php file for my JSON feed and already have all the events coming through in a WP_Query and looping them but I cant figure out how to grab the _category_color value from the term_meta.
$events = array();
$result = new WP_Query('post_type=event');
foreach($result->posts as $post) {
// To get the term ID I tried
$term = get_the_terms( $post->ID, 'eventcategory' );
$term_vals = get_term_meta($term,'_category_color');
$events = array(
'title' => $post->post_title,
'start' => date( 'Y-m-d', $data['start-date'] ),
'end' => date( 'Y-m-d', $data['end-date'] ),
'url' => get_post_permalink($post->ID),
'backgroundColor' => '#'.$term_vals["_category_color"][0],
'allDay' => false
);
}
echo json_encode($events);
exit;
I am just making a mess with the code and cant figure out how to do it right?

I have figured this out.
I was so close, yet, so far. I have commented the code to let you know how I figured it out.
$events = array();
$result = new WP_Query('post_type=event');
foreach($result->posts as $post) {
// I have to first get the Terms
$terms = get_the_terms( $post->ID, 'choirboss_eventcategory' );
//Then I had to loop through the terms to get the term_id
if($terms) {
foreach( $terms as $term ) {
$term_id = $term->term_id;
}
}
//Now I can grab my _category_color like this
$category_color = get_term_meta($term_id, '_category_color', true);
$events = array(
'title' => $post->post_title,
'start' => date( 'Y-m-d', $data['start-date'] ),
'end' => date( 'Y-m-d', $data['end-date'] ),
'url' => get_post_permalink($post->ID),
'backgroundColor' => '#'.$category_color,
'allDay' => false
);
}
echo json_encode($events);
exit;

Related

Get Custom Posts and put into option array - wordpress plugin

Trying to write a piece of code to expand a theme that I am using in Wordpress.
Basically, I want to get all custom post types and put it into an array for a select - the issue I am having is that I need to add the option values in the array and I cannot put a foreach loop in the array so not sure how to do this.
In the code below you will see the code:
'options' => array(),
This is where the custom posts need to be in the format:
'PostID' => esc_html__( 'Post Name', 'builder' ),
Here is my code:
function get_fields() {
$fields = array(
'get_post_names' => array(
'label' => esc_html__( 'Url Opens', 'builder' ),
'type' => 'select',
'option_category' => 'configuration',
'options' => array(),
'toggle_slug' => 'post_names',
'description' => esc_html__( 'Here you can choose whether or not your link opens in a new window', 'et_builder' ),
),
);
global $wpdb;
$custom_post_type = 'custom_post_name';
$results = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s and post_status = 'publish'", $custom_post_type ), ARRAY_A );
if ( ! $results )
return;
foreach( $results as $index => $post ) {
$fields['options'][] = array (
$post['ID'] => esc_html__( $post['post_title'], 'builder' ),
);
}
return $fields;
}
Any help would be much appreciated.
Thanks
Hopefully this may work
function generate_post_select($select_id, $post_type, $selected = 0) {
$post_type_object = get_post_type_object($post_type);
$label = $post_type_object->label;
$posts = get_posts(array('post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));
foreach ($posts as $post) {
echo $post->post_title;
}
}
$select_id is used as the name and id of the select, $post_type is the type you want to be made into the select and $selected is the post id you want selected in the select box.
Found a solution if anyone wants to know.
Changed the 'option' to be the following and removed the code from global $wpdb; down.
'options' => array_reduce( get_posts( 'post_type=custom_post&posts_per_page=-1' ), function( $result, $item ) {
$result[$item->ID] = $item->post_title;
return $result;
}),

Filter Gravity Forms Entries By Date

I am trying to filter (show) Gravity Forms Entries on the front end of my site between two certain dates. One being today (the current present date) and a custom date added within the backend of the website.
So only entries that were submitted between the custom date and the present date.
I have two variables storing these dates:
$new_start_date is the custom date, and
$end_date is the present days' date.
My code is outputting 200 entries but some of which are before the custom start date.
Below is my query code:
<?php
//Get the Form ID to get the entries from and store it within a varibale
$form_id = GFAPI::get_form(2);
// Get Dates
$date_page_id = get_field( 'update_dates_page_link', 'options', false, false );
$start_date = get_field( 'oak_submission_date', $date_page_id ); // date of the last submission date
$end_date = date( 'Y-m-d', time() ); //get today's date
// Convert start_date to match end_date format
$new_start_date = DateTime::createFromFormat( 'd/m/Y', $start_date );
$new_start_date = $new_start_date->format( 'Y-m-d' );
$search_criteria = array(
'status' => 'active',
'start_date' => $new_start_date,
'end_date' => $end_date,
'field_filters' => array(
array(
'key' => '53', // Trust name field
'value' => 'Oak Trust',
),
array(
'key' => '49', // Grant Made field
'value' => 'No',
),
),
);
$sorting = null;
$paging = array( 'offset' => 0, 'page_size' => 200 );
$entries = GFAPI::get_entries( $form_id, $search_criteria, $sorting, $paging );
// Code that outputs to the screen
foreach ( $entries as $entry) :
echo '<li>';
echo 'ID: '. $entry[68] .' : ' . $entry[2] .' : Submission Date: '. $entry[54];
echo '</li>';
endforeach;
GFAPI::get_form(2) doesn't return the Form ID it returns the Form. Try this:
$entries = GFAPI::get_entries( 2, $search_criteria, $sorting, $paging );

Show latest post from each author if post is not older than a month

I have a list of the authors (Wordpress) on the site that appears on every page so the list exists outside of the loop.
I managed to show every authors' image with their names but I would like to get their latest post title that links to the post. The post title should only show when the post is not older than a month.
Any help would be appreciated.
Thanks
<?php
global $wpdb;
$query = "SELECT ID, user_nicename from $wpdb->users WHERE ID != '1' ORDER BY 'ASC' LIMIT 20";
$author_ids = $wpdb->get_results($query);
foreach($author_ids as $author) :
// Get user data
$curauth = get_userdata($author->ID);
// Get link to author page
$user_link = get_author_posts_url($curauth->ID);
$post_link = get_permalink($curauth->ID);
// Set default avatar (values = default, wavatar, identicon, monsterid)
$main_profile = get_the_author_meta('mainProfile', $curauth->ID);
$hover_profile = get_the_author_meta('hoverProfile', $curauth->ID);
$award_profile = get_the_author_meta('awardProfile', $curauth->ID);
?>
You can use WP_Query to create a new loop for you instead. It accepts a cool date_query argument since version 3.7. Untested, but should work.
EDITED:
$args = array(
'showposts' => 1,
'orderby' => 'date',
'date_query' => array(
array(
'after' => array(
'year' => date( "Y" ),
'month' => date( "m", strtotime( "-1 Months" ) ),
'day' => date( "t", strtotime( "-1 Months" ) ),
),
'inclusive' => true,
)
) );
$query = new WP_Query( $args );
Then you can just run a regular loop
// run the loop with $query
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo 'Latest post: ' . get_the_title();
}
} else {
// no posts
}

PHP global $post not working! Trying to get the post ID in WordPress

I'm running into an issue where i'm trying to get the Post ID while trying to hook into a plugin function(function is inside my functions.php file)
For some reason global $post is not working!
My objective is to store the current posts, post ID into a variable where after the quiz is done, it will update a custom field.
Here is my code below. I'm echoing the $post->ID; just to test, but I'm not getting anything back.
add_action('ssquiz_finished', 'after_quiz_done', 10, 4);
function after_quiz_done( $quiz_id, $user_id, $questions_right, $total_questions )
{
global $post;
echo $post->ID;
}
Here is the actual 'ssquiz_finished' function/hook from the SS QUIZ plugin:
$output = ob_get_contents();
ob_end_clean();
$finish_screen .= $output;
$temp = new stdClass();
$temp->user_name = $info->user->name;
$temp->user_email = $info->user->email;
$temp->time_spent = time() - $info->started;
$wpdb->insert("{$wpdb->base_prefix}ssquiz_history",
array(
'user_id' => $info->user->id,
'quiz_id' => $info->quiz->id,
'meta' => serialize( $temp ),
'answered'=> $info->questions_counter,
'correct' => $info->questions_right,
'total' => $info->total_questions
),
array( '%d', '%d', '%s', '%d', '%d', '%d' ) );
if( $info->questions_counter == $info->total_questions ) {
// API
$percent = intval(strval($info->questions_right / $info->questions_counter * 100 ) ); //$info->questions_counter !=1
do_action( 'ssquiz_finished', $info->quiz->id, $info->user->id, $info->questions_right, $info->total_questions );
Any help would be greatly appreciated.

Wordpress custom loop displays posts based on the day...is posssible?

I would create a custom loop in the home page that will display only the posts that contains a specific word in its custom field.I'l try to explain it with an example.
EX. I got a website of a Basketball team and 2 custom post types called "COACh" and "TRAINING".
In "COACH" i add the coach and its skills.
In "TRAINING" i add the exrcise's name,the description and in the custom meta box i add the time,the coach,the duration and the day of the execution.
Now,suppose that it's Monday i would display all the TRAININGS that contains the day Monday in its custom field.
Is it possible without a plugin like event calendar????
IS IT RIGHT?
$today = date("l");
$args = array( 'post_type' => 'palinsesto', 'posts_per_page' => -1);
$palinsesto = get_posts( $args );
foreach ( $training as $post ) {
$day = get_post_meta( $post->ID, 'giorno ' ); // here day is key
if($day==$today)
while ($post->have_posts()) : $post->the_post(); ?>
Yes it is possible, I coded below roughly:
$args = array( 'post_type' => 'TRAINING', 'posts_per_page' => -1);
$training = get_posts( $args );
foreach ( $training as $post ) {
$day = get_post_meta( $post->ID, 'day ' ); // here day is key
if($day=='Monday')
echo $post->post_title.'<br />';
echo $post->post_content.'<br />';
}
this one is the solution for my question...it works great
$today = strftime('%A');
$day = get_post_meta( $post->ID, 'giorno ' );
$my_query = new WP_Query(array(
'post_type' => 'palinsesto',
'meta_query' => array(
array(
'key' => 'giorno',
'meta-value' => $value,
'value' => $today,
'compare' => '=',
))));
if (have_posts()) :
while ($my_query->have_posts()) : $my_query->the_post(); ?>

Categories