Finding the best results using a range of values

I'm currently working on a health and fitness website where a user inputs their daily calories and macro nutrients information (carbs, fat and protein). I have thousands of food items with custom meta for calories, carbs, fat and protein. I've developed a search that can find food items between set values using a meta query but now I would like to try and take it to the next level. For example, if a user wants to have 200g of carbs, …
Category: Web

If X Amount of Time Has Passed Since Post Was Published, Do Something

if ( human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) < strtotime( '7 days' ) ) { echo 'New!'; } I've also tried: if ( get_the_date( 'U' ) > strtotime( '-7 days' ) ) if ( get_the_date( 'U' ) < strtotime( '-7 days' ) ) if ( get_the_date( 'U' ) > strtotime( '7 days' ) ) if ( get_the_date( 'U' ) < strtotime( '7 days' ) ) if ( human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) > strtotime( '-7 …
Category: Web

Compare time value in WP_User_Query for sending emails

I am trying to setup a custom plugin that will compare the time (last activity of a user) and send emails to them. I am using the below WP_User_Query and a cron job function reminder_email() { // Get the contributors and authors who haven't logged in for 60 days - DO I NEED TO SET UP W3C Total Cache IN SOMEWAY IN ORDER FOR THE Query TO WORK? I DONT HAVE DB OR OBJECT CACHE ENABLED $remind_users = new WP_User_Query( …
Category: Web

Meta Query Compare with Date

$metas[] = array( 'key' => 'event_date', 'value' => array(date('10/10/2020'), date('10/11/2020')), 'compare' => 'BETWEEN', 'type' => 'DATE' Can you tell me, whats wrong here? How can i realise an comparison between two dates? Thanks! The whole funktion for the comparison is: public function getReports($options = array()) { $reportsCPTName = ktkReports::getCustomPostName(); $filiale = (isset($_REQUEST['setFiliale']) ? $_REQUEST['setFiliale'] : null); $kw = (isset($_REQUEST['setKW']) ? $_REQUEST['setKW'] : null); $kwyear = (isset($_REQUEST['setYEAR']) ? $_REQUEST['setYEAR'] : null); $datefrom = (isset($_REQUEST['datefrom']) ? $_REQUEST['datefrom'] : null); $dateuntil = (isset($_REQUEST['dateuntil']) …
Category: Web

Comparing two dates (ACF and current time)

I’m trying to compare two dates (the current date with an ACF datepicker one) to display specific content. What I’m trying to achieve is to display a phrase if the ACF date is lower than the current time (in few words if a service is expired). This is my code: $dataScadenza = get_field('data_di_scadenza'); $currentDateTime = date('d.m.y'); if($dataScadenza <= $currentDateTime) { echo '<a href="'.get_field('link_esito').'">'; echo '<button class="vedi_esiti">VEDI ESITI</button>'; echo '</a>'; } else { echo '<span class="ds_label">data scadenza</span>'; echo '<div class="ds_date">'.get_field('data_di_scadenza').'</div>'; } …
Category: Web

Comparing between a negative and positive number

I'm having some difficulty comparing between a negative and positive number. This code works fine when it's between two positive numbers but not when it's between a negative then a positive one. This is part of my 'meta_query': array_push($metaQuery, array('relation' => 'AND', array( 'key' => 'longitude', 'value' => array($minlng, $maxlng), 'compare' => 'BETWEEN', ), ) ); If for instance the $minlng is -1.5 and the $maxlng is 1.5. It will pass through values that equal -3. Here is a var_dump …
Category: Web

PHP question: how to combine syntax?

could someone please tell me where the mistake is in my PHP code? My PHP knowledge is extremely rudimentary although I have tried it with http://php.net/manual/de/language.operators.logical.php :-D "translated" it should mean something like: Single Post -> Show reading time: yes Custom Post Type "wpdmpro" -> Show reading time: no The function for the reading time works per se, but not in the way I put it together concerning the combination with the CPT. With my code the reading time is …
Category: Web

How to handle optional end date in compare clause in meta_query

I have a custom post type event in WordPress, and I need to query upcoming event posts comparing $current_date. Query conditions are : start_date is a valid date always end_date can be a valid date or null or empty string. IF end_date is a valid date in db record then compare end_date >= $current_date ELSE IF end_date is null or empty then compare start_date >=$current_date. Now If end_date was not optional , I could use below code to get desired …
Category: Web

Conditional statement for dates

Im trying to create a conditional statement comparing two dates. I have a ACF custom date field as one date, and then comparing against the current date. Below is what i have and iv echoed both $date and $currentdate and they both come out in the same format, so im not sure why it wont work. They both output in the format like: 20170105 which i beleive they need to be to compare. <?php $currentdate = current_time('Ymd'); $date = get_field('course_start_date', …
Category: Web

Compare date of user's last posts

I want to compare the date of a user's last two posts. I'm new to WordPress developing. I have written this code but I'm not sure if it's correct. Please help me in correcting it: $ID = $post->ID; $user_id = $post->post_author; $author_recent_posts = get_most_recent_post_of_user( $user_id ); $last_post_id = $author_recent_posts[1]->post_id; $last_post = get_post($last_post_id); $last_post_date = $last_post->post_date; $post_date = $post->post_date; if ( $post_date - $last_post_date > 24*60*60 ) return;
Category: Web

Conditional string comparison failing for basename/slug

I am trying to display a particular block of html for one page in particular. I have created a conditional statement that compares the basename of the page to my string. <?php if ( has_tag( 'Sponsor' ) && basename( get_permalink() != 'sponsor-one') ) : // show sponsor's footer if applicable ?> <div class="sponsor-footer <?php echo basename( get_permalink() ); ?>"> <p>Our text here</p> </div> <?php elseif ( has_tag( 'Sponsor' ) && basename( get_permalink() == 'sponsor-one') ) : // show sponsor one's …
Category: Web

Get previous posts list

I'll give up. Have searched and searched and tried and tried... Can't find any solution to list previous posts. On a single post: I want to show a list of the 10 previous posts, the 10 before the current post. So, not just a two prev and next links. Not the most recent posts that is. A list of the the 10 previous posts before the current post you are visiting. I'm trying to make a query and comparing post_date …
Category: Web

WP_Query using meta_query with LIKE doesn't return what it should

I am using this code to get stuff based on my custom field and custom content type. $searchTerm = 'bestafricanvideos'; $args = array( 'post_type' => 'sitemaps', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => 'cstm_sitemap_url', 'value' => $searchTerm, 'compare' => 'LIKE', ), ) ); print_r($args); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); $video_url = rwmb_meta('cstm_sitemap_url'); print_r($video_url); endwhile; else : echo "No video URL found"; exit; endif; wp_reset_postdata(); print_r($video_url); And …
Category: Web

Filter posts/pages by user_role array

I have made a code for to filter wich pages to show depending on your user_role. Everything was alright. But my customer created NEW roles with similars name/slug. So now, if some 1 have the role for exemple : Group APTNB, he will be able to see pages with role APTNB_CA because in the slug 'aptnb' is LIKE 'aptnb_ca' in my comparaison. I have try to explore the possibility to use 'IN' with this post : Meta_query compare operator explanation …
Category: Web

Comparison operator

I've build a dropdown box with a list of comparison strings. array( 'value' => array( __( 'Equal', 'js_composer' ) => '=', __( 'Does not equal', 'js_composer' ) => '!=', __( 'Greater than', 'js_composer' ) => '>', __( 'Greater than or equal to', 'js_composer' ) => '>=', __( 'LIKE', 'js_composer' ) => 'LIKE', __( 'NOT LIKE', 'js_composer' ) => 'NOT LIKE', __( 'IN', 'js_composer' ) => 'IN', // ...etc. ), ), if( $meta_query_value !== '' ) { $args['meta_query'][] = array( 'key' …
Category: Web

How to compare two posts including their meta fields on a scalable base?

I have set up a wordpress in a way that posts can be cloned and be altered by a different author. whenever this is the case, i want the administrator to be notified via an additional tab in the edit.php screen like Changes(5) where only posts are listed whose content and meta fields differ from those of their originals. to achive this i thought of writing some sort of hash whenever a post is saved to then only compare hashes. …
Category: Web

How to display checked posts on another page over AJAX? (like comparasion style)

I wanted to create an "comparasion" model for my WordPress Website, but gone bad at that. I cannot figure out how to ... On one page I have displayed post (custom post type, ex. "Products"). Next to the title of the post (product) is an checkbox. If user has checked for example two posts/products (ex. product a and product b), how to retrive the value (the $post->ID) of each checkbox or over ? How to save that checked $post->ID of …
Category: Web

About WP's save_post action

Quick question, in API's some times save_post is used(most of the time actually). Is save_post the equivalent of the query action INSERT INTO whereas save_post saves metadata and forces Wordpress to do the rest of the work when used correctly? Thanks in advance!
Category: Web

Custom Post Type or Shortcode, What to choose?

While developing a multipupose theme for mass audience, i’m facing an issue. I’m going to code staff listing section. Now i was wondering, what should i do? 1: Create dedicated Custom Post Type for staff; or 2: Create Shortcode for it. Now each decision has some pros and cons. a) Custom Post Type: Centralization, can call same group of listing on mutiple places without defining each time. Better staff order managerment No need to enter detail page link specifically Taxonomy …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.