Wordpress query order posts by meta key value - php

I would like to get ordered all my posts with multiple cutom fields (Wordpress 3).
Exemple, I've 2 custom couple meta_key/meta_value :
" order_submenuexpositions / numeric "
" display_submenuexpositions / boolean "
I tried like this, but the result it's not orderer by meta_value from "order_submenuexpositions" meta_key :
SELECT * FROM wp_posts
LEFT JOIN wp_postmeta wpostmetaOrder ON ( wp_posts.ID = wpostmetaOrder.post_id AND wpostmetaOrder.meta_key = 'order_submenuexpositions' )
LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
LEFT JOIN wp_term_relationships wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE wp_postmeta.meta_key = 'display_submenuexpositions'
AND wp_postmeta.meta_value = 'true'
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_term_taxonomy.taxonomy = 'category'
ORDER BY wpostmetaOrder.meta_value ASC,wp_postmeta.meta_value ASC
how can i do it ?
thank you !
ok resolved, it's because meta_value sql field type is longtext and i tried to order on integer value..it's doesn't work.you have to cast type on integer, like this :
SELECT * FROM wp_posts
LEFT JOIN wp_postmeta wpostmetaOrder ON ( wp_posts.ID = wpostmetaOrder.post_id AND wpostmetaOrder.meta_key = 'order_submenuexpositions' )
LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
LEFT JOIN wp_term_relationships wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE wp_postmeta.meta_key = 'display_submenuexpositions'
AND wp_postmeta.meta_value = 'true'
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_term_taxonomy.taxonomy = 'category'
ORDER BY CAST(wpostmetaOrder.meta_value AS SIGNED) ASC,wp_postmeta.meta_value ASC`

You can actually do this without using a complex SQL query for future reference.
Here is an Example:
$args = array(
'posts_per_page' => 250,
'post_type' => 'books',
'meta_query' => array(
array(
'key' => 'author',
'value' => $author_name,
'compare' => '=='
),
array(
'key' => 'publisher_id',
'value' => $publisher_id,
'compare' => '!=',
'type' => 'NUMBER'
)
)
);
$the_query = new WP_Query( $args ); while ( $the_query->have_posts() ) : $the_query->the_post();
// LOOP GOES HERE
endwhile;
You can add additional arrays inside "Meta_query" to dig even deeper and you can also learn about the different types of 'compare' and 'type' options available at the wordpress codex.
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

Related

Comparing WP dates

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

wordpress meta query always fails even if data is correct

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.

Custom query which sorts posts based on last comment date

I just want to sort posts from a standard query based on last comment date. I tried something like this (code below) but I can't get it right. PS: 'dzialy' => $cat this is custom taxonomy named dzialy and $cat is specified id of that category.
function forum_commentsjoin($join) {
global $wp_query, $wpdb;
if ($wp_query->query_vars['post_type']=='forum' && isset($wp_query->query_vars['dzialy'])) {
$join .= "LEFT JOIN $wpdb->comments ON $wpdb->comments.comment_post_ID=$wpdb->posts.ID";
}
return $join;
}
...and later on...
$args = array( 'post_type' => 'forum', 'dzialy' => $cat, 'posts_per_page' => $ilosc, 'orderby'=>'comment_date', 'order'=>'DESC', );
print_r($loop->query);
...after that…
Array ( [post_type] => forum [dzialy] => 1468 [posts_per_page] => 20
[orderby] => comment_date [order] => DESC )
You can use JOIN and wpdb class to run your raw sql queries
SELECT DISTINCT p.*
FROM
`wp_posts` p
LEFT JOIN `wp_comments` c ON(p.`ID`=c.`comment_post_ID`)
WHERE p.`post_status`='publish'
AND p.`post_type`='forum'
ORDER BY c.`comment_date` DESC
try this code,
select wp_posts.*,
coalesce(
(
select max(comment_date)
from $wpdb->comments wpc
where wpc.comment_post_id = wp_posts.id
),
wp_posts.post_date
) as mcomment_date
from $wpdb->posts wp_posts
where post_type = 'post'
and post_status = 'publish'
order by mcomment_date desc
limit 10
Reference : Ordering Wordpress posts by most recent comment
Hope this helps you

How to Add Category Filter in WordPress Post Query

I filtered the post's in my sidebar by using this code,
$mostlikequerystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'most_liked'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
ORDER BY $wpdb->postmeta.meta_value DESC
LIMIT 0 , 10";
This code working perfect , but now i want to add a category filter too..
for this i used $term_id
global $wpdb;
$term_id = get_term_by('slug','trailers');
$term_id->term_id;
echo $term_id;//Prints 12
$mostlikequerystr="SELECT $wpdb->posts.* FROM $wpdb->posts,$wpdb->postmeta
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.$wpdb->taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE ($wpdb->term_taxonomy.term_id = $term_id
AND $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'most_liked'
AND $wpdb->term_taxonomy.taxonomy = 'categories'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish')
LIMIT 0 , 10";
$tariler_post = $wpdb->get_results($mostlikequerystr, 'OBJECT');
echo $wpdb->show_errors();
but its not working for me and no error too ...
You are missing with conditional operator in your WHERE clause
WHERE ($wpdb->term_taxonomy.term_id = $term_id here
^^^^^
$wpdb->posts.ID = $wpdb->postmeta.post_id
Try this one by adding AND
$mostlikequerystr = "
SELECT $wpdb->posts.* FROM $wpdb->posts,$wpdb->postmeta
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.$wpdb->taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE ($wpdb->term_taxonomy.term_id = $term_id
AND $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'most_liked'
AND $wpdb->term_taxonomy.taxonomy = 'categories'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish'
ORDER BY $wpdb->postmeta.meta_value DESC
)
LIMIT 0 , 10";
If you are using WPDB class try to catch the errors
<?php $wpdb->show_errors(); ?>
Other way you can use WP's built in functions
$args = array(
'posts_per_page' => 10,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'most_liked',
'value' => '',
'compare' => '!='
),
'category__in' => 'id here or skip this argument'
'orderby' => 'meta_value_num',
);
$tariler_post=new WP_Query($args);

How to retrieve the sum from WordPress custom fields database, from the same column

This is my query. So i need to retrieve the sum from the values of budget meta key which are only in component 1 and in year 2009. How do i do this. Thanks in advance
SELECT SUM(
b.meta_value)
, b.meta_key
FROM wp_posts AS p, wp_postmeta AS b, wp_postmeta AS m, wp_postmeta AS n
WHERE (
p.ID = b.post_id
)
AND (
b.meta_key = 'budget'
)
AND (
m.meta_key = 'component'
AND m.meta_value = '1'
)
AND (
n.meta_key = 'component-year'
AND n.meta_value = '2009'
)
OK. So I think what you are saying is that you have posts with three custom fields; "component", "component-year", and "budget". You are trying to get a total of values of "budget" when component=1 and component-year=2009. If that is correct, then this works - it is not the most efficient but it works fine.
SELECT SUM(wp_postmeta.meta_value) AS total
FROM wp_posts
LEFT JOIN wp_postmeta ON (
wp_posts.ID = wp_postmeta.post_id
AND
wp_postmeta.meta_key = 'budget'
)
# get all the posts with a custom field of "component" of "1"
WHERE wp_posts.ID IN (
SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'component'
AND meta_value = '1'
)
# and all the posts with a custom field of "component-year" of "2009"
AND wp_posts.ID IN (
SELECT post_id
FROM wp_postmeta
WHERE meta_key = 'component-year'
AND meta_value = '2009'
);

Categories