PHP Function returns wrong date - php

I' am learning how to make a WordPress Plugin. I did make few simple plugins but not as complicated as this one. It's a Events Calendar. The var_dump from the function "nc_get_start_date()" on the page it outputs wrong dates.
The output from the var_dump(nc_get_start_date());
string(32) "1970-01-01,1970-01-01,1970-01-01"
This is what the function should return in real
23-12-2013, 25-12-2013, 26-12-2013
In the function.php on the plugin folder. This is the codes
/* Query to get the events post from the database */
function get_nc_events(){
global $post;
$query = new WP_Query(
array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC'
)
);
return $query;
}
/* Get the start date from the above function */
function nc_get_start_date(){
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
$wnc_start_date = $wnc_start_date[0];
$wnc_start_date = date("Y-m-d", strtotime($wnc_start_date));
$wnc_start_date_array .= "$wnc_start_date,";
endwhile;
return rtrim($wnc_start_date_array, ",");
}
When I write the code in page-caledar.php without the function it renders everything prefectly.
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
echo $wnc_start_date = $wnc_start_date[0] . "<br/>";
endwhile;

Problem Solved. Thanks everyone. The problem was in this function
/* Get the start date from the above function */
function nc_get_start_date(){
global $post;
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
$wnc_start_date = $wnc_start_date[0];
$wnc_start_date = date("Y-m-d", strtotime($wnc_start_date));
$wnc_start_date_array .= "$wnc_start_date,";
endwhile;
return rtrim($wnc_start_date_array, ",");
}
I didn't make global $post;

Related

How to auto update acf field based on posting date?

I have a custom post called Project.
I need to automatically update a field with 100 when the post date is over 7 days.
I tried the following coed in function.php but it doesn’t auto update the field.
Would you please let me know how to fix the code?
function update_active_based_on_date() {
// query to get all custom posts - project
$args = array(
'post_type' => 'project',
'posts_per_page' => -1
);
$query = new WP_Query($args);
// if posts are returned and more than 7days, update infosubmit field
if ($query->have_posts()) {
global $post;
$timestamp = get_the_time( 'U', $post->ID );
$diff = current_time('timestamp') - $timestamp;
while ($query->have_posts() && $diff > 604800) {
$query->the_post();
$field_key = "field_60836f942ae12";
$value = "100";
update_field($field_key, $value, $post->ID);
} // end while have_posts
wp_reset_postdata();
} // end if have_posts
} // end function
Thank you.
You can achieve this like:
function update_active_based_on_date() {
// query to get all custom posts - project
$args = array(
'post_type' => 'project',
'posts_per_page' => -1
);
$query = new WP_Query($args);
if ($query->have_posts()) {
global $post;
while ($query->have_posts()) {
$query->the_post();
// if posts are returned and more than 7days, update infosubmit field
$diff = time() - strtotime(get_the_date());
if($diff > 604800){
$field_key = "field_60836f942ae12";
$value = "100";
update_field($field_key, $value, $post->ID);
}
} // end while have_posts
wp_reset_postdata();
} // end if have_posts
} // end function
add_action('init','update_active_based_on_date');

CF7 Flamingo get messages from specific form

How can i get the messages from a certain form?
With this i can get all the inbound messages:
<?php
$args = array(
'post_type' => 'flamingo_inbound',
'post_status' => 'publish'
);
$query = new WP_Query($args);
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
echo '<article>';
echo get_the_title();
echo '</article>';
}
}
wp_reset_postdata();
?>
But i wanna show only the messages from a specific form for the current month. I search on Google and here but i can not find any example or anything for it so i hope somebody knows.
I have made a solution, maybe not the nicest php writing but it works:
<?php
$currentmonth = date('m');
$args = array(
'post_type' => 'flamingo_inbound',
'post_status' => 'publish',
'monthnum' => $currentmonth
);
$query = new WP_Query($args);
if( $query->have_posts() ){
echo '<ul>';
while( $query->have_posts() ){
$query->the_post();
$results = get_post_meta( get_the_ID() );
foreach($results['_meta'] as $result) {
$normaldata = unserialize($result);
if ($normaldata['post_id'] == '1234') { // The id of the post where the form is send from
$title = get_the_title();
echo '<li>' . $title . '</li>';
} else {
}
}
}
echo '</ul>';
}
wp_reset_postdata();
?>

Place IF ELSE statement inside Woocommerce product loop - Wordpress

I've got the following custom shortcode to display a range of products
add_shortcode( 'my_shortcode_name', 'on_sale_products' );
function on_sale_products() {
global $product, $woocommerce, $woocommerce_loop;
$args = apply_filters('woocommerce_related_products_args', array(
// this is working array, just empty for this example
)
);
$products = new WP_Query( $args );
ob_start();
woocommerce_product_loop_start();
while ( $products->have_posts() ) : $products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="on-sale">' . ob_get_clean() . '</div>';
}
I am trying to add a text message inside the loop that will say "No products to display" if there no products to show.
I'm struggling to correctly place the statement without getting a syntax error.
I've been fiddling around with the code like this:
add_shortcode( 'my_shortcode_name', 'on_sale_products' );
function on_sale_products() {
global $product, $woocommerce, $woocommerce_loop;
$args = apply_filters('woocommerce_related_products_args', array(
// this is working array, just empty for this example
)
);
$products = new WP_Query( $args );
ob_start();
woocommerce_product_loop_start();
if ( $products->have_posts() ) : $products->the_post() {
wc_get_template_part( 'content', 'product' );
} else {
echo '<div class="no-products">There are no products to display</div>';
}
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="on-sale">' . ob_get_clean() . '</div>';
}
But that's not correct.
Could you please point me to the right direction?
2 things:
1) You have removed the while loop.
2) There is an error in this line:
if ( $products->have_posts() ) : $products->the_post() {
It should be instead (in your code):
if ( $products->have_posts() ) {
$products->the_post();
So the following code, should be the correct way to get this working:
add_shortcode( 'my_shortcode_name', 'on_sale_products' );
function on_sale_products() {
global $product, $woocommerce, $woocommerce_loop;
$products = new WP_Query( apply_filters('woocommerce_related_products_args', array(
// this is working array, just empty for this example
) ) );
ob_start();
woocommerce_product_loop_start();
if ( $products->have_posts() ):
while ( $products->have_posts() ):
$products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
else:
echo '<div class="no-products">There are no products to display</div>';
endif;
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="on-sale">' . ob_get_clean() . '</div>';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This should work for you now…
Try this example,
function newsItems( $atts ) {
$news_query= null;
$args = array(
'post_type' => 'news',
'post_status' => 'publish',
'posts_per_page' => 10,
);
$news_query = new WP_Query( $args );
$output = '';
if ( $news_query->have_posts() ) {
$output .= '<div class="news-shortcode-posts">';
while ( $news_query->have_posts() ) : $news_query->the_post();
ob_start();
get_template_part( 'inc/news-item' );
$output .= ob_get_clean();
endwhile;
if( $show_archive == 'true' ) {
$output .= '<div class="full-width align-right">';
$output .= 'See All Archives';
$output .= '</div>';
}
$output .= '</div>';
}
return $output;
}
add_shortcode('teamsters-news', 'newsItems');
Hope this will helps you. for more information.
Using WordPress get_template_part in Shortcode Loop
get_template_part from inside a shortcode function

Include custom post type in Wordpress loop

Sorry, I think I read every post about this but cant get it to work.
I have a Wordpress custom post typ called "external" and I would like to show both the post from "external" aswell as my normal standard posts at the same time on my homepage.
This is my loop:
<?php
get_header();
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
How can I include the "external" posts in this code?
The theme then uses blocks to display content on the startpage, here is block8.php with a post query.
function block_eight_ajax_query($atts='') {
$args = array (
$is_ajax = 0;
if($atts==''){
$is_ajax=1;
$atts=$_GET;
if($atts['global_query']){
unset($atts['no_found_rows']);
unset($atts['suppress_filters']);
unset($atts['cache_results']);
unset($atts['update_post_term_cache']);
unset($atts['update_post_meta_cache']);
unset($atts['nopaging']);
}
}
$atts['is_ajax'] = $is_ajax;
$query = null;
$query = new WP_Query( $atts );
$html = '';
if ( $query->have_posts() ) {
ob_start();
$i = 1;
while ( $query->have_posts() ):
$query->the_post();
$atts['video'] = rd_field( 'abomb_post_video' );
$atts['extern'] = rd_field( 'extern' );
$atts['audio'] = rd_field( 'abomb_post_audio' );
$atts['gallery'] = rd_field( 'abomb_post_gallery' );
$atts['counter'] = $i;
block_grid_content($atts);
if ($atts['column'] == 'grid-12') { $divide = 1; }
else if ($atts['column'] == 'grid-3') { $divide = 4; }
else if ($atts['column'] == 'grid-4') { $divide = 3; }
else { $divide = 2; }
if ($i%$divide==0 && $divide!=1) {
echo '<div class="clear"></div>';
}
$i++;
endwhile;
if ($atts['nav'] == 'numbered') {
echo '<div class="grid-12">';
rd_pagination($query->max_num_pages);
echo '</div>';
}
wp_reset_postdata();
$html = ob_get_clean();
if($is_ajax==1){
echo $html;
exit();
}
return $html;
}
else{
if($is_ajax==1){
echo '-11';
exit();
}
return '';
}
}
Update: I added this code to functions:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( $query->is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'extern' ) );
return $query;
}
Now I can see the posts in for exampel the search result but I need the Query in Block8.php to fetch them aswell.
I Thanks!
To include a custom post type in the regular loop (ie. posts page) just add the following code to index.php before if ( have_posts() ) :
$args = array(
'post_type' => array('post', 'custom_post_type'),
'post_status' => 'publish',
);
$new_post_loop = new WP_Query( $args );
then modify the following two lines:
if ( have_posts() ) : change it to if ( $new_post_loop -> have_posts() ) :
and
while ( have_posts() ) : the_post(); to while ( $new_post_loop -> have_posts() ) : $new_post_loop -> the_post();
This solution avoids the problem of having the custom post type listed in the all posts screen on the backend, which #David.J's answer produces ;)
Add this to your functions.php file.
/**
* #param WP_Query $query
* #return WP_Query
*/
function add_my_custom_post_type( $query ) {
if ($query->is_main_query())
$query->set( 'post_type', array( 'post', 'external' ) );
return $query;
}
add_action( 'pre_get_posts', 'add_my_custom_post_type' );
Ref & duplicate: https://wordpress.stackexchange.com/questions/27104/how-to-display-regular-posts-custom-post-types-that-fall-under-a-category-usin
function add_my_custom_post_type( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ($query->is_main_query()) {
$query->set( 'post_type', array( 'post', 'some_custom_post_type_name' ) );
}
return $query;
}
}
add_action( 'pre_get_posts', 'add_my_custom_post_type' );
This is the correct way to write the code so as not to break the dashboard queries.

Wordpress Pagination - max_num_pages = 0

Wordpress ninjas... I require some help regarding pagination.
I have the following code :
global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array_merge( $wp_query->query_vars,
array(
'post_type' => 'attachment',
'posts_per_page' => 10,
'paged' => $paged
)
);
How come $query->max_num_pages returns 0 ?
And is there a way to alter the original archive query with the $query->max_num_pages ?
[EDIT]
In Addition to the above code I have the following
$output = '<div class="download-attachments left_content_container"><ul>';
//this does not retrieve anything
$query = new WP_Query( $args );
// this does retrieve posts
$attachments = get_posts( $args );
// this block is only used for testing purposes..
// ie. it is not being executed because the query does not return anything, Whilst get_posts() works
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
echo the_ID();
}
}
foreach ($attachments as $attachment) {
setup_postdata($attachment);
//get attachments metadata etc.
$url = wp_get_attachment_url( $attachment->ID );
$title = get_the_title( $attachment );
$caption = $attachment->post_excerpt;
$size = get_readable_size( filesize(get_attached_file( $attachment->ID )) );
$icon = get_icon_for_attachment( $attachment->ID );
$mime = get_post_mime_type( $attachment->ID );
$date_added = $attachment->post_date;
$filename = ( !$caption == '' ) ? $caption : $title ;
$description = ($attachment->post_content != '') ? '<span class="attachment-author"><span class="attachemnt-label">Added by: ' . $attachment->post_content . '</span></span>' : '';
$output .= '<li class="'.$mime.'">
<img class="attachment-icon" src="'.$icon.'" alt="pdf">
'. $filename .' '.$description.'
<span class="attachment-date"><span class="attachment-label">Date added: </span>'.$date_added.'</span>
<span class="attachment-size"><span class="attachment-label">Attachment size: </span>'.$size.'</span></li>' ;
}
$output .= '</ul></div>';
echo $output;
echo '<div class="paging pull-left">';
$paged = $wp_query->get( 'paged' );
if ( ! $paged) {/* do nothing */ ;}
else { bootstrap_pagination(); var_dump($wp_query->max_num_pages); // returns 0 }
echo '</div>';
You have a couple of problems here. You don't need to call the global $wp_query because you are not going to use it. Secondly, array_merge is totally out of place. This should not be used at all in this situation. Your code should look something like this
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'attachment',
'posts_per_page' => 10,
'paged' => $paged
)
);
$query = new WP_Query( $args );
For more info on custom queries with WP_Query have a look here

Categories