PHP in wordpress loop - advice needed please.

Associate
Joined
29 Sep 2010
Posts
2,013
Location
Cumbria
I am not great with php and javascript. I get by, bit for example spent an 2 hours today chasing a syntax error.

Anyway. I am trying to put a line showing how many days (via countdown) until a speedway fixture on the front page our website. I want the next three fixtures showing at all times throughout the season. I have manged to make the php work on the first one but the next two show an error.

The loop
Code:
<?php
  $temp = $wp_query;
  $wp_query = null;
  $wp_query = new WP_Query();
  $wp_query->query('showposts=3&orderby=date&order=asc&post_type=wcometsmeetings'.'&paged='.$paged);

  while ($wp_query->have_posts()) : $wp_query->the_post();
?>

and the 'countdown' ...

Code:
<?php

$d1=strtotime(get_field('countdown_date'));
$d2=ceil(($d1-time())/60/60/24);
echo "Coming up in " . $d2 ." days";
?>



The get_field is date-picker 'data' from advanced custom fields and changes for each fixture. This script is in the loop but will only output the first time. Is anyone able to make this work and explain it to me. I assume I have to make the $d1 $d2 increase each time to tie in with the number of articles showing ?.

I can't go by the article publish date as I have set the publish date to a year ago so they don't get 'scheduled' by wordpress.

I need them to not show after the time has expired. If thats difficult I can move them elsewhere when I input the results and match report.

Thanks in advance for any advice.
 
Last edited:
The loop
Code:
  $wp_query->query('showposts=3&orderby=date&order=asc&post_type=wcometsmeetings'.'&paged='.$paged);

You'll need to add a check in your query to only get countdown dates in the future.
Something like:

Code:
$today = date( 'Y-m-d' );
$args = array(
    'showposts' => 3,
    'orderby' => 'date',
    'order' => 'asc',
    'post_type' => 'wcometsmeetings',
    'meta_query' => array(
        array(
            'key' => 'countdown_date',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE'
        )
    )
):
$query = new WP_Query( $args );


get_field('countdown_date')
You can put the post id into the get_field method to get the field for a specific post. I think it's supposed to work without this in a loop but I've had problems with that in the past and always added the id in:
Code:
get_field('countdown_date', get_the_ID());
 
You'll need to add a check in your query to only get countdown dates in the future.
Something like:

Code:
$today = date( 'Y-m-d' );
$args = array(
    'showposts' => 3,
    'orderby' => 'date',
    'order' => 'asc',
    'post_type' => 'wcometsmeetings',
    'meta_query' => array(
        array(
            'key' => 'countdown_date',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE'
        )
    )
):
$query = new WP_Query( $args );



You can put the post id into the get_field method to get the field for a specific post. I think it's supposed to work without this in a loop but I've had problems with that in the past and always added the id in:
Code:
get_field('countdown_date', get_the_ID());
Thanks for the reply. Its causing a critical error onsite.

I am not sure where to put the ID bit... The getfield(countdown_date) is in the php countdown snippet, sepaarte from the query but within the loop.
 
Thanks for the reply. Its causing a critical error onsite.

I am not sure where to put the ID bit... The getfield(countdown_date) is in the php countdown snippet, sepaarte from the query but within the loop.
Oh, the error is probably because I named the variable $query and you've got $wp_query



For the countdown timer, replace the line
getfield(countdown_date)
with
getfield(countdown_date, get_the_ID())

Can you post the whole code? That would make it easier to see what's going on.
 
Last edited:
Sorted it via bodge for now. Made three wp queries... offset article number by 1 and changed the variables in php code for each loop. Will do the job for now.

I've got a lot to on there to get it ready so would you mind if I picked your brains at a later date over this get it done right as opposed to just bodged to work ? I am always looking to learn how to do things properly.
 
Sorted it via bodge for now. Made three wp queries... offset article number by 1 and changed the variables in php code for each loop. Will do the job for now.

I've got a lot to on there to get it ready so would you mind if I picked your brains at a later date over this get it done right as opposed to just bodged to work ? I am always looking to learn how to do things properly.
No problem. Tag me @touch when you post any questions so I see them.
 
Back
Top Bottom