Update wordpress post to draft if condition is true - php

I am using the below code to delete the rows from an ACF repeater field with expired date and when the number of rows reaches zero then update the post to draft mode or private mode.
But somethings are not woring (The draft thing is not working.)
$ap = get_post_meta($post->ID,'sub_seminars_0_start_date',true);
$startdate = date("Ymd", strtotime($ap));
$todaydate = date("Ymd");
if(strtotime($todaydate) > strtotime($startdate) && !empty($ap)){
$del_data = array(
'Ref' => 'sub_seminars_0_ref',
'Start date' => 'sub_seminars_0_start_date',
'End Date' => 'sub_seminars_0_end_date',
'Venue' => 'sub_seminars_0_venue',
'Fees' => 'sub_seminars_0_fees',
'CPE Credits' => 'sub_seminars_0_cpe_credits'
);
delete_row('sub_seminars', 1);
$row = count( get_field('sub_seminars') );
if ($row == 0) {
$postid = $post->ID; //Supply post-Id here $post->ID.
wp_update_post(array(
'ID' => $postid,
'post_status' => 'draft'
));
}
}
Please can anybody tell me what is wrong and how to fix it ?

$rows = get_field('sub_seminars');
$row_count = count($rows);
// $row_count = count($rows);
if ($rows == 0) {
$my_post = array(
'ID' => $post->ID,
'post_status' => 'draft'
);
wp_update_post($my_post);
}
use the above code it fixed the problem for me.

Related

WordPress Custom Taxonomy Term Meta Value

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;

Wordpress search and Filter API for IOS and Android

I am working on a project to apply filters on wordpress post...I have created JSON Output and Now I need to apply 5 filters that are as follows..
1. All
2. Today
3. WeekEnd
4. Next Seven Days
5. Date wise
Flow of the whole process...GEt Posts data in JSON--> Fetch This Data --> Create Filters---> User can Then Search as well as filter Data Further with normal wordpress functions....But I am not able to work out for fetching out posts by applying the Next Seven days filter and weekEnd..as the issue seems to be at my way of fetching the posts...What I am doing for now is
Code for Next Seven Days or last seven days...
<?php
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
//require_once("config.php" );
require_once("incformfunctions.php");
if($_SERVER['REQUEST_METHOD'] == "GET"){
$numberposts = isset($_REQUEST['numberposts']) ? $_REQUEST['numberposts'] : "-1";
$posttype = isset($_REQUEST['posttype']) ? $_REQUEST['posttype'] : "";
$securitycode = $_REQUEST['securitycode'];
$mode = $_REQUEST['mode']? $_REQUEST['mode'] : "ALL";
$taxonomy='art_categories';
if($securitycode == $secret)
{
$category_data=array();
if($mode=='recommended') //to get recommended post of posttype category
{
$args= array(
'numberposts' => 100,
'category' => 0,
'orderby' => 'date',
'order' => 'DESC',
'include' => array(),
'exclude' => array(),
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'events', //post name
'suppress_filters' => true,
'date_query' => array(
array(
'key' => 'event_start_date', //custom name start date
'after' => '1 week ago'
),
),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'event_editors_choice', //custom name event editor
'value' => true
),
'relation' => 'AND',
array(
'key' => 'event_recommended', //custom name event recommended
'value' => true
),
),
);
}
$myposts = get_posts( $args); // get all value in array
if($myposts) {
foreach ( $myposts as $post ) : setup_postdata( $post ); //and using foreach loop show all post type field
$image= get_the_post_thumbnail_url();
$featured_image =wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));
$start_date= get_post_meta($post->ID,'event_start_date',true);
$start_date=date('F j, Y', strtotime($start_date));
$end_date = get_field('event_end_date', $post->ID, false);
$end_date=date('F j, Y', strtotime($end_date));
$event_type = get_field('event_type', $post->ID, true);
$event_venue = get_field('event_venue', $post->ID, true);
$event_venue_address = get_field('event_venue_address', $post->ID, true);
$latitude = get_field('event_venue_latitute', $post->ID, true);
$longitude = get_field('event_venue_longitude', $post->ID, true);
$description_long = get_field('event_description_long', $post->ID, true);
$description_short = get_field('event_description_short', $post->ID,
true);
$gallery_address = get_field('gallery_address', $post->ID,
true);
if($gallery_address==false)
{
$gallery_address="";
}
$event_gallery = get_post_meta($post->ID,'gallery_address', true);
$venue_address= get_field('venue_address',$event_gallery,true);
$venue_postcode= get_field('venue_postcode',$event_gallery,true);
$venue_city= get_field('venue_city',$event_gallery,true);
$venue_location= get_field('venue_location',$event_gallery,true);
if($venue_location==false)
{
$venue_location="";
}
$venue=get_post_custom($post->ID);
$category=get_the_category($post->ID); //category
$excerpt=get_the_excerpt( $post );
$posttypes=get_post_type( $post );
$category_data[] = array('id' => get_the_ID (),'title' => get_the_title(),'excerpt' =>$excerpt,'featured_image' =>$featured_image,'image' =>$image,'event_type' =>$event_type,'event_venue' =>$event_venue,'event_venue_address' =>$event_venue_address,'event_latitude' =>$latitude,'event_longitude' =>$longitude,'start_date' =>$start_date,'end_date' =>$end_date,'posttypes' =>$posttypes,'tags'=>$tags,'description_long'=>$description_long,'description_short'=>$description_short,'venue'=>$event_gallery,'gallery_address'=>$gallery_address,'venue_address'=>$venue_address,'venue_postcode'=>$venue_postcode,'venue_city'=>$venue_city,'venue_location'=>$venue_location); // getting in all post array formate
wp_reset_postdata();
endforeach;
$data = $category_data;
$errcode=100;
$errstr='success';
}
else {
$errcode=-1;
$errstr='Post not found please check again..';
}
}
//for securitycheck
if ($securitycode !=$secret or $securitycode=='')
{
$errstr="unauthorise access";
}//end
}
else{
$errcode=-2;
$errstr='Request method not accepted';
}
#mysql_close($conn);
/ Output header /
#header('Content-type: application/json');
echo json_encode(array('errcode'=>$errcode,'errstr'=>$errstr,'data'=>$data)); // json create
die();
Any Help on what I am doing wrong??

Processing JSON decode to use in get_posts()

I have been struggling for a couple days with trying to use the data in a custom field to return posts. Here is my function that I have in functions.php. I am able to return my posts, except that they aren't limited to the ones defined in the $json variable. I can decode the json and return the array...but I can't seem to convert it in such a way that it fills in my array properly for the "posts__in" in the $dishes_arg.
Can anyone help me identify what I am doing wrong?
add_action ('woo_loop_before', 'hgf_home_menu');
function hgf_home_menu () {
if (is_home() || is_front_page()) {
wp_reset_query();
global $posts;
$menu_args = array(
'post_type'=>'single_menu',
'posts_per_page' => 1,
'meta_key' => 'orderby_date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'orderby_date', // Order-By Date field is upcoming
'value' => date("Y-m-d"),
'compare' => '>='
),
array(
'key' => 'orderby_date', // Order-By Date isn't more than two weeks out
'value' => date("Y-m-d", strtotime( "+2 weeks")),
'compare' => '<='
)
),
);
// Get menu that meets criteria
$menus = new WP_Query( $menu_args );
// Show menu that meets criteria
if ( $menus->have_posts() ) {
while ( $menus->have_posts() ) {
$menus->the_post();
}
wp_reset_postdata();
// Get the menu's product/post listing
$json = '[{"id":"435"},{"id":"527"},{"id":"563"},{"id":"568"}]';
$array = json_decode($json);
$include = array();
foreach($array as $a) {
$include[] = $a->ID;
}
$args = array(
'posts_per_page' => -1,
'include' => $include);
$posts = get_posts($args);
$dish_args = array(
'post_type' => 'product',
'post__in' => $posts,
);
// Get dishes in menu listing
$dishes = get_posts( $dish_args );
if ($dishes) {
foreach ($dishes as $dish) {
}
}
} else { // no posts found }
/* Restore original Post Data */
wp_reset_postdata();
}
}
You jSon property is id not ID
$include[] = $a->ID;
Should be
$include[] = $a->ID;
Where/why is there jSon here? Is this coming from something else.
Otherwise that could just be a simple delimitated list or even a normal PHP array.
I figured it out...turns out I just had to cut out a bunch of code that was getting in the way: $include = array() ... $args = array() ... $dish_args = array() ... $dishes = get_posts(). Here is the corrected portion:
$json = '[{"id":"435"},{"id":"527"},{"id":"563"},{"id":"568"}]';
$dishes = json_decode($json);
if ($dishes) {
foreach ($dishes as $dish) {
// Echo titles and permalinks
}
}

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
}

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