
Can you provide a screenshot? People can be wary of clicking 3rd party links to unknown sites.
Having not clicked, if the site is WooCommerce(?), you could look to use either a hook or add the relevant template to your theme.
If its a specific theme, you would need to locate the necessary file and add your code for the custom field (price?) in the relevant place.

Also add some debugging in, check your script outputs what it should:
<?php
# Our include
require_once('../../../wp-load.php');
global $wp_query;
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => -1,
'post_type' => 'groeiprocessen',
'fields' => 'ids', //not sure what to add here
'post_status' => array('publish'),
'paged' => $paged
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()) :
while ($wp_query->have_posts()) : $wp_query->the_post();
$post_id = get_the_ID();
echo '<p>Post ID: '.$post_id.'</p>';
$datetime1 = new DateTime();
echo '<p>Date time 1: '.$datetime1.'</p>';
$datetime2 = get_field( "datum" );
echo '<p>Date time 2: '.$datetime2.'</p>';
$difference = $datetime1->diff($datetime2);
echo '<p>Difference: '.$difference.'</p>';
$verschil = "difference " . $difference->days . " days ";
echo '<p>Difference: '.$verschil.'</p>';
update_post_meta( $post_id, 'datum2', $verschil );
endwhile;
endif;

Check this post
Because you are passing string whereas date_diff expects datetime object,
$date_expire = '2014-08-06 00:00:00';
$date = new DateTime($date_expire);
$now = new DateTime();
echo $date->diff($now)->format("%d days, %h hours and %i minuts");
I’d suggest you check the values you’ve got stored. My hunch is $datetime2 isn’t the right format

Ah! Add this to the top of the file (after the <?php):
# Our include
require_once('../../../wp-load.php');
I think you can uncomment the other bits:
<?php
# Our include
require_once('../../../wp-load.php');
global $wp_query;
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => -1,
'post_type' => 'groeiprocessen',
'fields' => 'ids', //not sure what to add here
'post_status' => array('publish'),
'paged' => $paged
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()) :
while ($wp_query->have_posts()) : $wp_query->the_post();
$post_id = get_the_ID();
$datetime1 = new DateTime();
$datetime2 = get_field( "datum" );
$difference = $datetime1->diff($datetime2);
$verschil = "difference " . $difference->days . " days ";
update_post_meta( $post_id, 'datum2', $verschil );
endwhile;
endif;
This assumes you’ve added the file (cron.php) into your theme dir. If you’ve added it within a directory in your theme (like includes), you need to amend the path to wp-load.php

You can try:
<?php
global $wp_query;
#$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => -1,
'post_type' => 'groeiprocessen',
'fields' => 'ids', //not sure what to add here
'post_status' => array('publish'),
#'paged' => $paged
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()) :
while ($wp_query->have_posts()) : $wp_query->the_post();
$post_id = get_the_ID();
$datetime1 = new DateTime();
$datetime2 = get_field( "datum" );
$difference = $datetime1->diff($datetime2);
$verschil = "difference " . $difference->days . " days ";
update_post_meta( $post_id, 'datum2', $verschil );
endwhile;
endif;

Yep, pretty much it!
So if you call it cron.php and add that to your theme file, then go to:
https://www.domain.com/wp-content/themes/theme-name/cron.php
It should run
Just backup your DB before running it, just to be sure.
Then check your posts to ensure it has the right dates.
As I say, depending on post quantity, this may lead to time out issues in the future! I’d therefore look into batch processing which can run without as many issues

Actually, this won’t work. Reason being, you need access to the order to get the date for comparison!
So you have options:
1) When you run the date difference checker the first time (as per your other post), I think I’d be inclined to have another custom field which stores the value from:
$date_created = $order->get_date_created();
OR
2) When you run the date difference code for the first time (as per your other post), you store if post ID of the order. That way, you can query the order post ID to get the order completed date.
In my mind, option 1 is far easier and easier to implement:
1) Just an another update_meta line to your other code
2) Add a get_field on the code I posted above to get the order completed date
As I say, my only concern is how many posts you may need to return each day, may timeout or become slow

If you had a file like:
<?php
global $wp_query;
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => -1,
'post_type' => 'your_cpt',
'fields' => 'ids',
'post_status' => array('publish'),
'paged' => $paged
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()) :
while ($wp_query->have_posts()) : $wp_query->the_post();
$post_id = get_the_ID();
$datetime1 = new DateTime();
$date_created = $order->get_date_created();
$datetime2 = new DateTime($date_created);
$difference = $datetime1->diff($datetime2);
$verschil = "difference " . $difference->days . " days ";
update_post_meta( $post_id, 'datum2', $verschil );
endwhile;
endif;
You can then run a cron to ping that URL, it would in theory then run the script.
However, depending on the volume of posts, over time, may be less efficient.
One other option is to look at running batch processing to do the same, but this is a lot more involved.
The above code is of course untested!

I suspect you would need to run a CRON daily which would trigger a script/function.
That script/function can then run the code to update your date.
Where does $post_id come from? Is it a custom post or a WooCommerce order?
Is it a published post or in a set status?
Basically, your script could simply be a WP Query that loops whatever the post type is, it should then update the post meta as you add your code above in the loop

Ok, so the following is based on your code and my suggestion.
Before doing anything, backup your site.
You already have the code in your functions file to create the film custom post type.
So now you can add the following to your functions file:
########################
# Add Taxonmoies
########################
function register_taxonomies() {
$taxonomies = array(
array(
'slug' => 'genres',
'single_name' => 'Genre',
'plural_name' => 'Genres',
'post_type' => 'film',
'hierarchical' => true,
),
);
foreach( $taxonomies as $taxonomy ) {
$labels = array(
'name' => $taxonomy['plural_name'],
'singular_name' => $taxonomy['single_name'],
'search_items' => 'Search ' . $taxonomy['plural_name'],
'all_items' => 'All ' . $taxonomy['plural_name'],
'parent_item' => 'Parent ' . $taxonomy['single_name'],
'parent_item_colon' => 'Parent ' . $taxonomy['single_name'] . ':',
'edit_item' => 'Edit ' . $taxonomy['single_name'],
'update_item' => 'Update ' . $taxonomy['single_name'],
'add_new_item' => 'Add New ' . $taxonomy['single_name'],
'new_item_name' => 'New ' . $taxonomy['single_name'] . ' Name',
'menu_name' => $taxonomy['plural_name']
);
$rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] );
$hierarchical = isset( $taxonomy['hierarchical'] ) ? $taxonomy['hierarchical'] : true;
register_taxonomy( $taxonomy['slug'], $taxonomy['post_type'], array(
'hierarchical' => $hierarchical,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => $rewrite,
));
}
}
add_action( 'init', 'register_taxonomies' );
##########################
# Ajax filter Films
##########################
function filter_films() {
$args = array(
'post_type' => 'film',
'posts_per_page' => -1,
);
if( isset( $_POST['genre'] ) )
$args['tax_query'] = array(
array(
'taxonomy' => 'genres',
'field' => 'id',
'terms' => $_POST['genre']
)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
?>
<h2><?php the_title(); ?></h2>
<?php
endwhile;
wp_reset_postdata();
else :
echo 'No films found matching your criteria.';
endif;
die();
}
// Fire AJAX action for both logged in and non-logged in users
add_action('wp_ajax_filter_films', 'filter_films');
add_action('wp_ajax_nopriv_filter_films', 'filter_films');
The first part creates the custom taxonomy for Genres.
The second part handles the filtering which we’ll come to next.
Create a page called films.
Now create a template file called page-films.php, from here you can add the following code:
<?php
/**
* @package WordPress
Template Name: Films
**/
get_header();
?>
<div id="filters">
<?php
$genres = array(
'taxonomy' => 'genres',
);
$genres = get_categories( $genres );
?>
<?php foreach($genres as $genre): ?>
<input type="checkbox" value="<?php echo $genre->term_id; ?>" class="form-check-input genre" />
<label class="form-check-label" for="<?php echo $genre->cat_name; ?>"><?php echo $genre->cat_name; ?></label>
<?php endforeach; ?>
<a href="#" class="btn" id="reset_genre">Reset Genres</a>
</div><!-- /filters -->
<div id="ajax_filter_films">
<?php
$args = array(
'post_type' => 'film',
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
?>
<h2><?php the_title(); ?></h2>
<?php
endwhile;
wp_reset_postdata();
else :
echo 'No films found matching your criteria.';
endif;
?>
</div><!-- /ajax_filter_films -->
<?php
get_footer();
You can assign the template films to the page, but due to naming conventions, should work. I always add it as a fallback.
Now, in your footer you can add:
<script type="text/javascript">
//filter the films
jQuery(function($){
filter_data();
function filter_data()
{
$('.filter_data').html('<div id="loading" style="" ></div>');
var action = 'filter_films';
var genre = get_filter('genre');
$.ajax({
type : "POST",
data : { action:action, genre:genre},
dataType : "html",
url : '<?php echo admin_url('admin-ajax.php');?>',
success : function(data) {
//alert(this.data);
jQuery("#ajax_filter_films").html(data);
},
error : function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
return false;
}
function get_filter(class_name)
{
var filter = [];
$('.'+class_name+':checked').each(function(){
filter.push($(this).val());
});
return filter;
}
$('.form-check-input').click(function(){
filter_data();
});
$("#reset_genre").click(function(){
// get the current selected values
var genre = [];
$('.genre:checked').each(function(){
genre.push($(this).val());
});
// loop through and remove the selected checkobox
var i;
for (i = 0; i < genre.length; i++) {
$(".genre:checkbox[value="+genre[i]+"]").parent().removeClass('selected'); //remove the highlighted lable
$(".genre:checkbox[value="+genre[i]+"]").prop("checked", false); //uncheck the hidden checkbox
}
// reset the array
genre = [];
console.log(genre); //debug
// update the filter
filter_data();
});
});
</script>
I’d probably flush the permalinks!
You can now go to films within wp-admin and you should see a menu option called Genres.
Add the ones you need
Edit your films and assign one/multiples.
Go to the films page on the front end (its basic but gives you the idea).
You should now see all the films you’ve added plus a bunch of checkboxes.
Tick one or many and the results should filter!
The code is completely tested and works with a fresh WP install and the Twentytwentyone theme.

Why don’t you create your own custom genre taxonomy? That leaves the existing categories for the blogs. Assuming I’ve understood

Hi @daisuke
Here you go, this code has been tried and tested and confirmed working:
add_action('pre_get_posts', 'filter_posts_by_acf');
function filter_posts_by_acf( $query ) {
if(is_admin()){
return;
}
$meta_query = $query->get('meta_query');
if( isset($_GET['coup_de_coeur']) ){
$meta_query = [];
$meta_query[] = array(
'key' => 'coup_de_coeur',
'value' => $_GET['coup_de_coeur'],
'compare' => 'LIKE',
);
}
$query->set('meta_query', $meta_query);
return $query;
}
Adding your code initially, I got an error but its because I already had a function with the same name, so changed it.
I then added some validation and a quick tidy.

Hmm
Right after the function line, try something like
if ( is_admin() || !$query->is_main_query() )
return $query;

Or try return $query;

Can you comment out or remove the return at the bottom. See if that solves it.
It all looks OK.

Critical error would be something else, not the = or LIKE
What does the critical error say? Does it provide a line of code? If so, what line and what code is nearby?

A checkbox stores as a serialised array, change the compare from = to LIKE
That I believe should do it

What if you try:
<?php include( locate_template( 'includes/loop.php', false, false ) ); ?>
Just change the directory from includes to what you need and loop.php to the name of your file.
Does that work for you?

What do you mean by classification? Is this a custom post type?
If so, you could look at Query posts by custom fields.

The ACF plugin would allow you to add custom fields to your custom post type.

Just tested and works fine
Here’s what I did:
1) Create an ACF text field called Random String
2) This creates the label random_string
3) For testing, I assigned this field to User Role is equal to All
4) I added the below code to my functions.php file:
<?php
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
####generate the random number
#get current date/time
$date = date('YmdH:i:s');
#randmoise
$randomise = ($date * 25);
update_user_meta($user_id, 'random_string', $randomise ); #change random_string to your ACF field label!!!
}
5) I registered a new user
6) I then clicked to edit the user, scrolled down to my new Random String field, this was the result: 50527810375
Code and entire process tried and tested!

Did you even try adding the code to your functions file? It should be pretty much good to go subject to swapping the field name and testing.
Plugins whilst help, try to avoid adding them if you can.
Better to add code you know than constant 3rd party plugins which may become vulnerable (in my honest opinion)

You can just switch the code to something like:
<?php $genres = get_the_terms( $post->ID, 'genres' );
if( $genres ): ?>
<h2>Genres</h2>
<ul>
<?php foreach( $genres as $genre ): ?>
<li><?php echo $genre->name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
That will then output any genres associated to that film/post

In theory, you should be able to add it below:
$post_id = wp_insert_post($new_post);
Reason being, you’ve accessed the order info, you’ve created the post and have the ID.
The only part you need to check is that date formats are correct and you change the custom_field name to your field.
Code is untested, so you may need to check, test and debug by echoing out various points to see if it works

<?php if( $molecules_of_the_month = get_terms( array( 'taxonomy' => 'molecules_of_the_month' ) ) ) :
foreach( $molecules_of_the_month as $month ) :
$summaryimage = get_field('summary_preview_image', 'term_' .$month->term_id );?>
<?php print_r($summaryimage);?>
<?php endforeach;
endif;
?>
Try that on its own, if it works, integrate it into your code
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.