strtotime with variable in wordpress - wordpress

I've tried to change this code:
function filter_where( $where = '' ) {
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-365 days')) . "'";
return $where;}
add_filter( 'posts_where', 'filter_where' );
into this one:
function filter_where( $where = '' ) {
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-'.$timestamp.' days')) ."'";
return $where;}
add_filter( 'posts_where', 'filter_where' );
You may noticed that I've tried to put variable $timestamp inside strtotime. However the code doesn't work. Did I do the correct syntax to put the variable within the strtotime PHP function?
I appreciate any kind of help.

It is the correct code if $timestamp contains a positive integer.
However, in the context of that function and without the global keyword, $timestamp is undefined and null.
Do you mean to pass that variable as an argument of and_where()?

Related

How to display Wordpress Posts for each day in a table in descending order (Recent Post First)?

I am trying to Group each post in a date-wise order inside a table.
Please check the attached Image
function filter_where( $where = '' ) {
// where post_date > today
$where .= " AND post_date >= '" . date('Y-m-d') . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
Use this code

How to filter BuddyPress member loop by ACF field?

PHP newb here, looking for some guidance. I am working with BuddyPress and Advanced Custom Fields (ACF). I have an ACF field 'new_user' with a value of true/false. I am trying to filter my BuddyPress Members Loop to only display users with a value of 'new_user' = true.
There are 2 code samples here.
The standard BP Members Loop. My thought here, is how do I first query my users by ACF ‘new_user’ = true and then start the bp member loop?:
if ( bp_has_members() ) :
// some code goes here
endif;
while ( bp_members() ) : bp_the_member();
//OUTPUT MEMBERS LIST HERE
endwhile;
This is a BP function to filter by Buddypress extended user fields. The idea here I believe is to replace the code in the middle specific to xprofile_get_field with the proper ACF code:
function my_custom_ids( $field_name, $field_value = '' ) {
if ( empty( $field_name ) )
return '';
global $wpdb;
$field_id = xprofile_get_field_id_from_name( $field_name );
if ( !empty( $field_id ) )
$query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id;
else
return '';
if ( $field_value != '' )
$query .= " AND value LIKE '%" . $field_value . "%'";
/*
LIKE is slow. If you're sure the value has not been serialized, you can do this:
$query .= " AND value = '" . $field_value . "'";
*/
$custom_ids = $wpdb->get_col( $query );
if ( !empty( $custom_ids ) ) {
// convert the array to a csv string
$custom_ids_str = 'include=' . implode(",", $custom_ids);
return $custom_ids_str;
}
else
return '';
}
Of course, I am open to solving this in another way as well. I hope this is clear.

How to search in WordPress content instead of title only

I'm using the following query/code for a custom WordPress search-plugin.
add_filter( 'posts_where', 'title_like_posts_where', 10, 2 );
function title_like_posts_where( $where, &$wp_query ) {
global $wpdb;
if ( $post_title_like = $wp_query->get( 'post_title_like' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( $wpdb->esc_like( $post_title_like ) ) . '%\'';
}
return $where;
}
But it is only searching in the post-titles, where I want it to search in both the title and the content.
If I simply change the word 'title' with the word 'content', it doesn't work.
Anyone knows what I have to do in order to make the query search in both the title and the content?

apply filters on wordpress get_posts

I wish to limit the get_posts() result by dates
i tried using the following but it is not working for me...
can anyone tell me if im doing this correctly ? and why is not performing as expected ?
add_filter( 'posts_where', 'filter_dates_between' );
$args=array(
'post_type'=>'log',
'suppress_filters' => false,
'post_status'=>'private',
'numberposts'=> -1,
'meta_key'=>'_wpcf_belongs_agent_id','meta_value'=>$agentId);
$logs=get_posts($args);
remove_filter( 'posts_where', 'filter_dates_between' );
here is the filter function
function filter_dates_between( $where ='' ) {
global $dateFrom,$dateTo,$wpdb;
$where .= $wpdb->prepare( " AND post_date >= %s", $dateFrom );
$where .= $wpdb->prepare( " AND post_date <= %s", $dateTo );
return $where;
}
Your code is good. I think the problem comes from the dates you pass in which are treated like strings.
Make sure in the end its in this example format
"AND post_date >= '2009-03-01' AND post_date <= '2009-03-15'"
Or simply
"AND cast(post_date as datetime) between cast... AND cast...

Comparing timestamps in WP_Query

I can't figure out how to compare timestamps in the WP_query. On the site, classes/events are entered as posts with meta added for the date they begin. In calendar_functions.php, $begtime is defined as the timestamp for the date the class/event starts (not the date the post is created) and $today is defined as today's timestamp. I've tested both of those and they do return correct values.
The below code works fine, it displays a random class/event... except that past classes/events show up. I want to be able to only display items that happen from today on. Its not an option to go in and delete classes/events that have already happened.
Not sure how to handle the >= part.
<?php include('calendar_functions.php'); ?>
<?php $my_query = new WP_Query('"$begtime >= $today"&posts_per_page=1&cat=5,43&orderby=rand');
if (have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
I will also need to be able to compare two sets of dates, as some classes have two sessions, ex. $begtime >= $today -or- $begtime2 >= $today. ($begtime2 is also already defined).
you need something like this.
function filter_where( $where = '' ) {
// posts 30 to 60 days old
$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

Resources