I´m trying to show posts with a meta field "fecha_inicio" that contains a younger date with this format "dd/mm/yyyy" with 1 variable that also has a date with the same format "dd/mm/yyyy".
My code is the next one:
$args = array(
'posts_per_page' => 30,
'post_type' => 'programa',
'paged' => get_query_var( 'paged' ),
'meta_query' => array(
array(
'key' => 'fecha_inicio',
'value' => $fecha,
'type' => 'date',
'compare'=> '<'
),
),
);
This doesn´t return me anything. I have posts for example with date 10/10/2016 and 01/01/2016, and I´m trying the query with a date in the middle 05/05/2016, and It´s not returning me anything
I have printed the variable and shows the correct date, and also the other dates are correct on the database, dunno what I am missing here.
Also the field has the same name, that in the table post_meta
Trying to show the SQL Query
SELECT SQL_CALC_FOUND_ROWS
wp_posts.ID
FROM
wp_posts
INNER JOIN
wp_postmeta
ON
(
wp_posts.ID = wp_postmeta.post_id
)
WHERE
1 = 1 AND(
(
wp_postmeta.meta_key = 'fecha_inicio' AND CAST(wp_postmeta.meta_value AS DATE) > '01/05/2095'
)
) AND wp_posts.post_type = 'programa' AND(
wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private'
)
GROUP BY
wp_posts.ID
ORDER BY
wp_posts.post_date
DESC
LIMIT 0, 30
Related
The second meta_query parameter is being ignored in this query.
$query = new WP_Query(array(
'post_type' => 'events',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'event_date',
'value' => date('Ymd'),
'compare' => '>=',
),
array(
'key' => 'end_date',
'value' => date('Ymd'),
'compare' => '>=',
)
)
));
There is no mention of end_date in the resulting SQL
SELECT SQL_CALC_FOUND_ROWS inx9uju_posts.ID
FROM inx9uju_posts
INNER JOIN inx9uju_postmeta ON ( inx9uju_posts.ID = inx9uju_postmeta.post_id )
INNER JOIN inx9uju_postmeta AS mt1 ON ( inx9uju_posts.ID = mt1.post_id )
WHERE 1=1
AND ( inx9uju_postmeta.meta_key = 'event_date' AND ( mt1.meta_key = 'event_date' AND CAST(mt1.meta_value AS SIGNED) > '20180703' ) )
AND inx9uju_posts.post_type = 'events'
AND (inx9uju_posts.post_status = 'publish'
OR inx9uju_posts.post_status = 'acf-disabled'
OR inx9uju_posts.post_status = 'private')
GROUP BY inx9uju_posts.ID
ORDER BY inx9uju_postmeta.meta_value+0 ASC LIMIT 0, 30
I've spent some time checking syntax and referring to the codex but I can't see what is wrong with my code. I can't see any reason why it would be ignoring the end_date part of the meta_query parameters.
UPDATE
In this particular case I was overriding the meta_query part of the query in a separate function which was hooking into pre_get_posts. Once I updated that function, the issue was resolved.
If this is stored as an AFC, you must use the ACF syntax of Ymd (20180703).
You must also realise that WordPress can detect the BETWEEN meta_compare to detect two dates as such.
I've following query -
$query = array(
'post_type' => 'accessory',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'accessory-bike',
'value' => array("33"),
'compare' => 'IN'
)
)
);
And now I've this record in database -
520 is the ID for accessory post type. It still returns 0 results. I can't figure where the hell did I do wrong ? Stupid wordpress.
SELECT wp_posts.*
FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1=1
AND wp_posts.post_type = 'accessory'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private')
AND ( ( wp_postmeta.meta_key = 'accessory-bike' AND CAST(wp_postmeta.meta_value AS SIGNED) IN ('33') ) )
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC
The value of accessory-bike on post 520 isn't 33; it's a serialized string.
I can see you're using Advanced Custom Fields so all you need to do is pick a different field type that doesn't save data in a serialized string. You may find that something like a custom taxonomy is more appropriate.
Every post has two post meta: year and month (custom)
I need SQL query to pull all posts that have
wp_postmeta.meta_key = year AND wp_postmeta.meta_value = 2013
AND AT THE SAME TIME another meta_key / meta_value pair
wp_postmeta.meta_key = month AND wp_postmeta.meta_value BETWEEN 1 AND 12 ;
SELECT * FROM wp_postmeta
WHERE (meta_key = 'agam_post_options_year' AND meta_value = 2013)
OR (meta_key = 'agam_post_options_month' AND meta_value BETWEEN 0 AND 12 )
GROUP BY meta_value;
This is something I tried and a couple of more variations but it doesn't do much... this specifically gives me one post that has year 2013 and 12 posts that have either 1 or ... to 12 for month field}
Please replace _PUT_YOUR_POST_ID_FIELD_HERE_ by something appropriate field from that table and try to run...
SELECT Y.meta_value, M.meta_value FROM wp_postmeta as Y
JOIN wp_postmeta AS M USING (_PUT_YOUR_POST_ID_FIELD_HERE_)
WHERE (Y.meta_key = 'agam_post_options_year' AND Y.meta_value = 2013)
AND (M.meta_key = 'agam_post_options_month' AND M.meta_value BETWEEN 0 AND 12 )
GROUP BY Y.meta_value, M.meta_value;
OK It was all good. The reason it took out the "duplicates" was because we didn't group by Y.meta_id and instead we used meta_value
If you don't really need a pure SQL query and you're getting these posts for using them in WordPress, you could use meta_query array passed as a part of args to WP_Query class or get_posts() function.
$args = array(
'posts_per_page' => -1,
'post_type' => 'your-post-type', // default 'post'
'meta_query' => array(
relation => 'AND', // default 'AND' it might be 'OR'
array(
'key' => 'agam_post_options_year',
'value' => '2013',
'compare'=> '=',
'type' => 'NUMERIC' // see docs for more types
),
array(
'key' => 'agam_post_options_month',
'value' => array( 0, 12 ),
'compare' => 'BETWEEN',
'type' => 'NUMERIC' // see docs for more types
)
)
);
$posts = get_posts( $args ); // returns an array of posts objects
//OR
$query = new WP_Query( $args ); // returns a new WP_Query object
Hope it helps! : )
SELECT DISTINCT date(p.post_date) as post_date
FROM $wpdb->posts p
LEFT JOIN $wpdb->postmeta pm ON p.ID = pm.post_id
WHERE p.post_status='publish'
AND p.post_type IN ('%s')
ORDER BY p.post_date DESC
Here it reads all dates of the posts. My problem is that query should exclude the posts which has meta_value(it can be anything) of which meta_key="develop" . How can i write the query for that ?
I feel it will be in this form for wp_query
$args[ 'meta_query' ]= array (
'relation'= >' AND ',
array (
'key' = > 'respect'
),
array (
'key' => 'develop',
'compare' => 'NOT EXISTS'
)
);
$query = new Wp_query($args);
So you're trying to get all posts that don't have a meta key equal to "develop"?
You could do this:
$query = new WP_Query( array(
'meta_key' => 'develop',
'meta_compare' => 'NOT EXISTS'
) );
Also, see this note from the Codex if you're using a version of Wordpress less than 3.9:
"(Note: Due to bug #23268, value is required for NOT EXISTS comparisons to work correctly prior to 3.9. You must supply some string for the value parameter. An empty string or NULL will NOT work. However, any other string will do the trick and will NOT show up in your SQL when using NOT EXISTS. Need inspiration? How about 'bug #23268'.)"
I am using magic fields and have defined a custom post type called collection with a field called sort, where I input a number I would like to sort the custom posts by.
My WP_Query arguments is simply:
'post_type' => 'collection',
'meta_key' => 'sort',
'order_by' => 'meta_value',
'order' => 'ASC'
Which, upon using print_r on the $query result reveals the following mysql statement:
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = 'collection' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND (wp_postmeta.meta_key = 'sort' ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC LIMIT 0, 10
So I am confused why the generated mysql still contains ORDER BY wp_posts.post_date when I am explicitly stating it should sort by wp_postmeta.meta_key = 'sort' and its corresponding meta_value?
'post_type' => 'collection',
'meta_key' => 'sort',
'orderby' => 'meta_value',
'order' => 'ASC'
order_by = orderby
Also it's not called magic fields but custom fields
http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters