I am trying to fetch another table column by WP_Query but it does not work.
Note: I can easily do this by $wpdb->get_results( 'custom_sql' ) but I want to do it by WP_Query
My code
function join_my_query( $join, $wp_query ) {
global $wpdb;
if ( $wp_query->get( 'custom_logic' ) === true ) {
$join .= " LEFT JOIN $wpdb->postmeta as pm ON $wpdb->posts.ID = pm.post_id ";
}
return $join;
}
add_filter( 'posts_join', 'join_my_query', 10, 2 );
$query = new WP_Query(
array(
'custom_logic' => true,
'post_type' => 'post',
)
);
It's giving me SQL like below, which does not select any column from the joined table!
Generated SQL
SELECT
SQL_CALC_FOUND_ROWS wp_posts.ID
FROM
wp_posts
LEFT JOIN wp_postmeta as pm ON wp_posts.ID = pm.post_id
WHERE
1 = 1
AND (
(
wp_posts.post_type = 'post'
AND (
wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'future'
OR wp_posts.post_status = 'draft'
OR wp_posts.post_status = 'pending'
)
)
)
ORDER BY
wp_posts.post_date DESC
LIMIT
0, 10
It does not select any column from my joined table. I have use var_dump($query->posts[0]) which return a WP_Post object and it has no column from my joined table. How I can get columns from my joined table with WP_Query like below
My Goal
$post = $query->posts[0];
$post->meta_key; // from my joined table, not work
$post->pm->meta_key; // pm is alias used in join, still not work
According to your expected result, the posts_join filter is not required. You can do this with the posts_clauses filter easily!
add_filter( 'posts_clauses', 'modify_post_clauses_callback' , 10, 2 );
function modify_post_clauses_callback( $clauses, $wp_query ) {
if ( $wp_query->get( 'custom_logic' ) === true ) {
global $wpdb;
$clauses['fields'] = $clauses['fields'] . ', pm.meta_key';
$clauses['join'] = "LEFT JOIN {$wpdb->postmeta} as pm ON {$wpdb->posts}.ID = pm.post_id";
}
return $clauses;
}
$query = new WP_Query(
array(
'custom_logic' => true,
'post_type' => 'post',
)
);
$post = $query->posts[0];
$post->meta_key;
Related
I have posts where the author is assigned as Co-Author. If i want to show them on my author page, they are not listed. Because of this condition in the query:
AND (wp_posts.post_author = 53)
How can i remove it? Or is there any better solution?
public static function pre_get_posts($query)
{
if ($query->is_author() && $query->is_main_query()):
$author = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
$authorPostIDs = self::getAuthorsPostIDs($author->ID);
$coAuthorPostIDs = self::getCoAuthorsPostsIDs($author->ID);
$postIDs = array_unique(
array_merge($coAuthorPostIDs, $authorPostIDs), SORT_REGULAR
);
$query->set('post__in', $postIDs);
$query->set('post_status', 'publish');
endif;
}
My full query:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1
AND wp_posts.ID IN (349956,327745)
AND (wp_posts.post_author = 53)
AND wp_posts.post_type = 'post'
AND ((wp_posts.post_status = 'publish'))
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10
The products have the attribute Tempo. This is a text field in which to enter the number.
How to sort products by the minimum and maximum attribute value?
using such url ?filtering=1&filter_min-tempo=100&filter_max-tempo=150
Is it possible to create an attribute of type integer?
All attributes are stored as text, but you can cast them to the type you need.
Look at this code:
function isequal($v1,$v2) {
return intval($v1) == intval($v2);
}
function filter_loop_shop_post_in( $array ) {
if ( !array_key_exists('min_tempo',$_GET) && !array_key_exists('max_tempo',$_GET) ) return $array;
global $wpdb;
$min = isset( $_GET['min_tempo'] ) ? floatval( $_GET['min_tempo'] ) : 0;
$max = isset( $_GET['max_tempo'] ) ? floatval( $_GET['max_tempo'] ) : 9999999999;
$query = "
SELECT DISTINCT
tr.object_id
FROM
{$wpdb->term_relationships} AS tr
LEFT JOIN
{$wpdb->term_taxonomy} AS tt
ON
tr.term_taxonomy_id = tt.term_taxonomy_id
LEFT JOIN
{$wpdb->terms} AS t
ON
tt.term_id = t.term_id
WHERE
tt.`taxonomy` = 'pa_tempo' AND CAST(t.name AS DECIMAL(10, 1)) BETWEEN $min AND $max";
$raw_results = $wpdb->get_results( $query );
if (!sizeof($raw_results)) return $array;
$results = array();
foreach ($raw_results as $res) {
$results[] = intval($res->object_id);
}
if (!sizeof($array)) return $results;
return array_uintersect($results, $array, 'isequal');
};
add_filter( 'loop_shop_post_in', 'filter_loop_shop_post_in', 10, 1 );
It works for me in WooCommerce 2.6.14.
How to limit query to 6 in woocommerce product price filter short-code [products_by_price min="100" max="300"]? The code below is based on woocommerce product short-code which unfortunately does not support per_page. In this case I need to limit query to 6 to avoid showing all products.
add_shortcode( 'wc_products_price_range', 'wc_products_price_range' );
function wc_products_price_range( $atts, $content, $shortcode ) {
if ( class_exists( 'WooCommerce' ) ) {
$shortcodes = new WC_Shortcodes();
if ( is_array( $atts ) ) {
$min = (int) $atts['min'];
$max = (int) $atts['max'];
if ( $min && $max ) {
$and = "meta_value BETWEEN $min AND $max";
} else {
if ( $min ) {
$and = "meta_value >= $min";
} elseif ( $max ) {
$and = "meta_value <= $max";
}
}
if ( $and ) {
global $wpdb;
$query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_price' AND $and";
$ids = $wpdb->get_col( $query );
if ( ! empty( $ids ) ) {
$atts['ids'] = implode( ",", $ids );
}
}
}
return $shortcodes->products( $atts );
}
}
After a quick scan of WC shortcode class I have came across this :
public static function products( $atts ) {
$atts = shortcode_atts( array(
'columns' => '4',
'orderby' => 'title',
'order' => 'asc',
'ids' => '',
'skus' => ''
), $atts );
it means you can pass 'column' attribute along with ids where you can limit posts per page.
and in this query :
$query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_price' AND $and";
you can use limit 0,6 with order by clause.
Hope this helps.
OR
random rows:
SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 1;
or you can pass 'rand' to attributes in shortcode.
I added some custom fields (dates) in my admin columns. I try to make them sortable but in order to do that I need to convert them from string to date. My naive and simple code obviously doesn't work... Can someone point out my error(s) ?
add_action( 'pre_get_posts', 'orderby_date' );
function orderby_date( $query ) {
$orderby = $query->get( 'orderby');
if( 'start' == $orderby ) {
$query->set('meta_key','class_start');
$query->set('orderby',"STR_TO_DATE(meta_value,'%m/%d/%Y')");
}
}
This is what you are looking for:
add_filter( 'posts_clauses', 'manage_wp_posts_be_qe_posts_clauses', 1, 2 );
function manage_wp_posts_be_qe_posts_clauses( $pieces, $query ) {
global $wpdb;
if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {
$order = strtoupper( $query->get( 'order' ) );
if ( ! in_array( $order, array( 'ASC', 'DESC' ) ) )
$order = 'ASC';
switch( $orderby ) {
case 'class_start':
$pieces[ 'join' ] .= " LEFT JOIN $wpdb->postmeta wp_rd ON wp_rd.post_id = {$wpdb->posts}.ID AND wp_rd.meta_key = 'class_start'";
$pieces[ 'orderby' ] = "STR_TO_DATE( wp_rd.meta_value,'%m/%d/%Y' ) $order, " . $pieces[ 'orderby' ];
break;
}
}
return $pieces;
}
You can read more about it here: http://wpdreamer.com/2014/04/how-to-make-your-wordpress-admin-columns-sortable/
Can someone tell me what wrong with this query.
if ( isset( $_GET['lokacija'] ) && !empty( $_GET['lokacija'] ) ) {
$lokacija = $_GET['lokacija'];
} else { $lokacija = ''; }
if ( isset( $_GET['tip'] ) && !empty( $_GET['tip'] ) ) {
$tip = $_GET['tip'];
} else { $tip = ''; }
if ( isset( $_GET['sobe'] ) && !empty( $_GET['sobe'] ) ) {
$sobe = $_GET['sobe'];
} else { $sobe = ''; }
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args2 = array(
'posts_per_page' => 10,
'post_type' => 'nekretnine',
'paged' => $paged,
if ($lokacija != '') {
'meta_query' => array(
array (
'key' => 'lokacija',
'value' => $lokacija.''
),
)
}
);
$wp_query = new WP_Query( $args2 );
This code gives me error
Parse error: syntax error, unexpected T_IF, expecting ')' in
*/wp-content/themes/gs/page-nek-pretraga.php on line 23;
Line 23 is line that starts with if ($lokacija)...
What i want to do is to use multiple meta_query that i can get from php get (www.blabla./com/page1/?lokacija=foo&tip=foo&sobe=3)
But, i want it only if lets say $lokacija is not empty. Same for other two (possible 5-6 later) fields.
You can not include if condition in array. Whatever you are trying to achieve with above code is you can achieve with this following code.
$args2 = array(
'posts_per_page' => 10,
'post_type' => 'nekretnine',
'paged' => $paged,
);
if ($lokacija != '') {
$args2['meta_query'] = array(
array (
'key' => 'lokacija',
'value' => $lokacija.''
),
);
}
To check for multiple custom fields we have to join the meta table twice.
So the copy of the table is joined with a different temporary table name.
global $wpdb; $query = " SELECT * FROM {$wpdb--->prefix}posts
INNER JOIN {$wpdb->prefix}postmeta m1
ON ( {$wpdb->prefix}posts.ID = m1.post_id )
INNER JOIN {$wpdb->prefix}postmeta m2
ON ( {$wpdb->prefix}posts.ID = m2.post_id )
WHERE
{$wpdb->prefix}posts.post_type = 'post'
AND {$wpdb->prefix}posts.post_status = 'publish'
AND ( m1.meta_key = 'date' AND m1.meta_value > '2010-12-05 00:00:00' )
AND ( m1.meta_key = 'date' AND m1.meta_value < '2010-12-12 00:00:00' ) AND ( m2.meta_key = 'some_other_meta_value' AND m2.meta_value != '' ) GROUP BY {$wpdb->prefix}posts.ID
ORDER BY {$wpdb->prefix}posts.post_date
DESC;
For More Details Visit : http://realtuts.com/write-custom-wordpress-sql-query-multiple-meta-values/
";