Wp_query post__not_in not working - mysql

I'm trying to build a query using wp_query where certain posts id must be excluded. Here is the code that i'm using:
$args = array( 'posttype' => 'post', 'posts_per_page' => 1, 'cat' => 14, 'post__not_in ' => array(71,1), 'orderby' => 'menu_order', 'order' => 'ASC' , 'post_status' => 'publish' );
$the_query = new \WP_Query( $args );
However wp_query is still returning me the posts with those ID's.
Here is the pastebin with the wp_query object http://pastebin.com/gjayN4Yc.
As for the wp_query->request i'm having the following:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (14,15) ) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order ASC LIMIT 0, 3
Am i doing something wrong or is this a core bug?
Thank you.

I ran your code and it worked fine for me, except there is an extra space after 'post__not_in '. Do you see it? Make sure you have the key as 'post__not_in'.
Also, there are a couple of things to mention:
It's 'post_type' and not 'posttype.
'menu_order' is not available for posts. That is allocated for hierarchical post types, such as pages. You set that field in the Page Order UI, which is not available (by default) to posts.
Here is the revised code:
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'cat' => 14,
'post__not_in' => array( 71, 1 ),
'orderby' => 'date',
'order' => 'ASC',
'post_status' => 'publish',
);
$the_query = new WP_Query( $args );
if ( ! $the_query->have_posts() ) {
// maybe echo a message that none were found
return;
}
while( $the_query->have_posts() ) {
$the_query->the_post();
// do your business logic here
// then call the view file to render the HTML
}
wp_reset_postdata();
The SQL query then becomes:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE 1=1 AND wp_posts.ID NOT IN (71,1) AND
( wp_term_relationships.term_taxonomy_id IN (14) ) AND
wp_posts.post_type = 'post' AND
((wp_posts.post_status = 'publish'))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date ASC LIMIT 0, 1

Have you tried adding a second underscore after post in the $args array key - so it's post__not_in (2 consecutive underscores) rather than post_not_in as per the WP_Query docs?

Try this one
$aTest = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => -1, //all post
'order' => 'ASC',
'cat' => 14,
'post__not_in' => array(27),
'orderby' => 'date',
'post_status' => 'publish'
) );
while ( $aTest ->have_posts() ) : ($aTest ->the_post()) ;
$aTest Image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$title = get_the_title();
$content = get_the_content();
endwhile;

Related

I need mysql query for wordpress for get ordered list

I need to get ordered list by three fields.
$sql = "
SELECT
SQL_CALC_FOUND_ROWS $wpdb->posts.ID
FROM
$wpdb->posts
INNER JOIN
$wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )
INNER JOIN
$wpdb->postmeta AS mt1 ON ( $wpdb->posts.ID = mt1.post_id )
WHERE
1=1
AND
(
( mt1.meta_key = 'pub_series' AND mt1.meta_value = 'Book' )
OR ( mt1.meta_key = 'pub_series' AND mt1.meta_value = 'Book chapter' )
OR ( mt1.meta_key = 'pub_series' AND mt1.meta_value = 'Journal Article' )
)
/*AND
(
mt3.meta_key = 'forthcoming'
)*/
AND
$wpdb->postmeta.meta_key = 'pub_year'
AND
$wpdb->posts.post_type = 'publication'
AND
$wpdb->posts.post_status = 'publish'
GROUP BY
$wpdb->posts.ID
ORDER BY
/*FIELD(mt3.meta_key, 'forthcoming') DESC,
FIELD(mt3.meta_value, 'null') DESC,*/
//FIELD($wpdb->postmeta.meta_value,1),
$wpdb->postmeta.meta_value DESC,
$wpdb->posts.post_date DESC
";
$total = count($wpdb->get_results($sql));
$offset = ( $paged * $posts_per_page ) - $posts_per_page;
$results = $wpdb->get_results( $sql . "
LIMIT
$offset, $posts_per_page" );
This is query ordered list by two fields: [custom_field] pub_year (possible values 2018, 2017...) and [delautl wp post date field] post_date. It is ok.
But now I need to show items with [custom_field] forthcoming = 1. Problem that there three states if this field:
- items with forthcoming = 1
- items with forthcoming = 0
- and items where custom field forthcoming not exist
I need to get ordered list with firts items forthcoming = 1 and other should be ordered by pub_year and date. How I can do this. Tell me if you need some more info. Thanks a lot.
I was get needed ordered list with adding for all posts meta key forthcoming = 0 with update meta method. So all fields have this field.
Next I was used wp_query meta_query args:
$pub_series = 'Book,Book chapter,Journal Article';
$search_args = array();
if (!empty($pub_series)) {
$pub_series_arr = explode(",", $pub_series);
if (!empty($pub_series_arr)) {
$search_args_add = array(
'relation' => 'OR'
);
foreach ($pub_series_arr as $pub_series_el) {
$adv_search_query_el = trim($pub_series_el);
if (!empty($adv_search_query_el)) {
$search_args_add[] = array(
'key' => 'pub_series',
'value' => $adv_search_query_el,
'compare' => '='
);
}
}
$search_args['meta_query'][] = $search_args_add;
}
}
$search_args['meta_query'][] = array(
'relation' => 'OR',
'forthcoming_clause' => array(
'key' => 'forthcoming',
'value' => '1'
),
'forthcoming_clause0' => array(
'key' => 'forthcoming',
'value' => '0'
)
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => array(
'forthcoming_clause' => 'DESC',
'meta_value' => 'DESC',
'date' => 'DESC'
),
'meta_key' => 'pub_year',
'paged' => $paged
);
$args = array_merge($args, $search_args);
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) : ...

Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column

Im trying to order a wp_query by meta_value
Im using the following arguments
array(
'post_type' =>'event',
'posts_per_page' => -1,
'meta_query' => array(
'dates_query' => array(
'key' => 'dates',
'value' => date(time()),
'compare' => '>='
)
),
'orderby' => 'dates_query',
'order' => 'ASC',
'paged' => 1
);
This is the request that is generated by wp_query
SELECT wp_posts.* FROM wp_posts
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1=1 AND ( ( wp_postmeta.meta_key = 'dates'
AND wp_postmeta.meta_value >= '1514960717' ) )
AND wp_posts.post_type = 'event'
AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'completed'
OR wp_posts.post_status = 'acf-disabled')
GROUP BY wp_posts.ID
ORDER BY CAST(wp_postmeta.meta_value AS CHAR) ASC
Unfortunatly the results are wrong and when trying to execute the query manualy im getting the following error:
Expression #1 of ORDER BY clause is not in GROUP BY clause and contains non-aggregated column
I know there is an option to disable "only_full_group_by" but im wondering if that is the best practice in this case
Thanks
Not sure why you have used data_query But You should try by directly meta_query on key-value compare. Let me know if it works.
array(
'post_type' => 'event',
'posts_per_page'=> -1,
'paged' => 1,
'meta_key' => 'dates',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'dates',
'value' => date(time()), //<-- Cross check if your date has same format.
'compare' => '>=',
'type' => 'DATE'
)
)
);
I can't comment on wp_query, but, for reference, here's an example of a valid query (although I can't imagine why you want to sort integers as if they were strings):
SELECT DISTINCT p.*
FROM wp_posts p
JOIN wp_postmeta m
ON p.ID = m.post_id
WHERE m.meta_key = 'dates'
AND m.meta_value >= '1514960717'
AND p.post_type = 'event'
AND p.post_status IN('publish','completed','acf-disabled')
ORDER
BY CAST(m.meta_value AS CHAR) ASC;

How to add order by on Custom field on query_posts

query_posts(array(
'post_category' => 'xyz',
'post_type' => 'abc',
'posts_per_page' =>'16',
'paged'=> $paged,
'orderby' => 'post_title',
'order' => 'ASC'
));
I want to order the records being fetched based on the title of the post in ascending order.
When i print the result using
echo 'query : '.$GLOBALS['wp_query']->request;
I notice that the order by field is post_date
I have pasted the query below:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (7) ) AND wp_posts.post_type = 'abc' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC LIMIT 0, 16
It should work with get_posts():
$args = array(
'category' => 'xyz',
'post_type' => 'abc',
'posts_per_page' =>16,
'orderby' => 'title',
'order' => 'ASC'
);
$myposts=get_posts($args);
echo '<ul>';
foreach($myposts as $post){ setup_postdata( $post );
echo '<li>'.get_the_title().'</li>';
}
wp_reset_postdata();?>
echo '</ul>';
But I'm not sure what 'paged'=> $paged, is.
I found an answer to this, it should be:
query_posts(array(
'post_category' => 'xyz',
'post_type' => 'abc',
'posts_per_page' =>'16',
'paged'=> $paged,
'orderby' => 'title',
'order' => 'ASC'
));

Select the posts with OR operator in the same meta_key AND operator for differemt meta_key

I have stucked with a mysql query.
I want to select the posts having OR operator between same meta_key then AND operator on different meta_key i have tried :-
SELECT p.ID, p.post_title, pm.meta_value FROM wp_posts p
LEFT JOIN `wp_postmeta` pm ON p.ID =pm.post_id where
((pm.meta_key = 'cost_for_two' AND pm.meta_value IN(".$prices."))
AND (pm.meta_key = 'bar_or_no_bar' AND pm.meta_value='".$bars."') ) LIMIT 0,7
Want to select the post who have bars and prices selected.
as you said "Want to select the post who have bars and prices selected."
You can do this by Wordpress query too.
try this code:
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'cost_for_two',
'value' => $prices,
),
array(
'key' => 'bar_or_no_bar',
'value' => $bars,
)
),
'posts_per_page' => 7
);
$query = new WP_Query( $args );

posts_per_page is set to 1, returns 50 posts

I'm trying to get the latest post in a custom post type:
$query = new WP_Query(array(
'post_type' => 'some_custom_post_type',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
));
For some weird reason, this returns 50 posts. I echoed the SQL from $query->request, and I can see that the LIMIT is set to 50:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1
AND wp_posts.post_type = 'some_custom_post_type'
AND (
wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private'
)
ORDER BY wp_posts.post_date DESC
LIMIT 0, 50
Switching post_limits instead of posts_per_page yields the same weird result.
Why is this happening, and how can I fix it?
try
$query = new WP_Query(array(
'post_type' => 'some_custom_post_type',
'posts_per_page' => 1,
'showposts' => 1,
'orderby' => 'date',
'order' => 'DESC'
));