I have been trying for a couple of hours to make this work - but for some reason its simply to difficult for me. I have a custom post_type 'house', and I want to find the post_id of my custom post_type with a meta_key and certain meta value.
Lets say i want to find post_id for a house with
meta_key='house_id'
meta_value='231sd1223'
How exactly would i do that with wp->query?
Here you have the query even with a loop. However, querying meta values is making more DB queries, consider looping throught "house" post type and than doing something only if meta_value is equal the house number.
// WP_Query arguments
$args = array (
'post_type' => array( 'house' ),
'post_status' => array( 'publish' ),
'meta_query' => array(
array(
'key' => 'house_id',
'value' => '231sd1223',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
global $wpdb;
$results = $wpdb->get_results( "select post_id, meta_key from $wpdb->postmeta where meta_value = '231sd1223.'", ARRAY_A );
Related
some of my products missing their images, what I'm trying to do is to make a query that will give me only products with the default image (woocommerce place holder image).
this is what i have tried :
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query', array(
array(
'key' => '_thumbnail_id',
'value' => '5',
'compare' => '=='
)
)
);
i found place holder image id using this function :
attachment_url_to_postid("/wp-content/uploads/woocommerce-placeholder.png");
the query returns every single product I have, and not only those with the placeholder image, what causes it, and is there a better way?
You can check using meta_key _thumbnail_id. Try the below query.
global $wpdb;
$post_ids = $wpdb->get_results( "
SELECT ID FROM $wpdb->posts
WHERE ID NOT IN (
SELECT post_id from $wpdb->postmeta
WHERE meta_key = '_thumbnail_id'
)
AND post_type = 'product'
AND post_status = 'publish'
" );
echo "<pre>"; print_r( $post_ids ); echo "</pre>";
I have there the code I'm using, and I'm trying to get the posts in the order of the IDs from the $menus array but don't do that, he gave me the posts from the newest to oldest...I have tried to use order with DESC but the array did not change.
$menus = array(105, 54, 111);
$args = array(
'post__in' => $menus,
'orderby' => 'ID',
'order' => 'DESC',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
$i = 1;
while ( $query->have_posts() ) : $query->the_post();
do_action('fwp_before_post_content');
get_template_part('extend-helpers/' . $layout);
do_action('fwp_after_post_content');
$i++;
endwhile;
else:
get_template_part('extend-helpers/content', 'none');
endif;
UPDATE:
The posts are from different categories.
UPDATE II:
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.ID IN (105,54,111) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.menu_order, FIELD( wp_posts.ID, 105,54,111 ) LIMIT 0, 1000"
Why I have ORDER BY wp_posts.menu_order? because in $args I don't have orderby menu_order..
You need to add the "orderby" item in the args you pass WP_Query.
You can find the documentation here
Have you seen the WP_query loop? https://code.tutsplus.com/tutorials/mastering-wp_query-using-the-loop--cms-23031. It uses $args=ASC and DSC and 'orderby ' =>'id' too.
for the first part, you want to use post__in also in orderby line as Juan suggested in his comment. From the documentation: 'post__in' - Preserve post ID order given in the 'post__in' array. So, your code should look like this
$menus = array(55, 53, 57); // I have used mine ids
$args = array(
'post__in' => $menus,
'orderby' => 'post__in',
);
$query = new WP_Query( $args );
For the second part, which is more important, why do you have ORDER BY wp_posts.menu_order. You can find answer also in comments. The query is being altered by something (theme or plugin), check pre_get_posts hook, try switching theme, etc.
I have tested this code and it is working, so it is ordering posts as they are in array $menus.
I am trying to exclude some posts with a custom meta_key within the hook pre_get_posts but for some reason is not working, the posts are not getting excluded. Taxonomies to be excluded work, but posts no.
add_action('pre_get_posts' , 'changeCourseCountry');
function changeCourseCountry($query){
global $wpdb;
$tax_query_merge = array(
'relation' => 'AND',
array(
'taxonomy' => 'course_category',
'field' => 'slug',
'terms' => array('short-courses', 'mega-course'),
'operator' => 'NOT IN'
),
);
$tax_query = array_merge($tax_query, $tax_query_merge);
$exclude = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_video_course'"); // here I get an array of posts
$query->set('tax_query' ,$tax_query);
$query->set('post__not_in', $exlcude);
return $query
}
Please use the following instead of getting the column data,
$exclude = $wpdb->get_row("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_video_course'"); // here I get an array of posts
So it will be ,
add_action('pre_get_posts' , 'changeCourseCountry');
function changeCourseCountry($query){
global $wpdb;
$tax_query_merge = array(
'relation' => 'AND',
array(
'taxonomy' => 'course_category',
'field' => 'slug',
'terms' => array('short-courses', 'mega-course'),
'operator' => 'NOT IN'
),
);
$tax_query = array_merge($tax_query, $tax_query_merge);
$exclude = $wpdb->get_row("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_video_course'",ARRAY_A); // here I get an array of posts
$query->set('tax_query' ,$tax_query);
$query->set('post__not_in', $exlcude);
return $query;
}
Also if you need multiple IDs then you need to use get_results and loop the array in foreach.
I also added ARRAY_A to $wpdb query to retrive the array.
I've managed to save a string into Wordpress database. The problem is that I am only able to insert the string when this code runs in a single url. I've got around 1500 posts in my database so I'd like to make it work for all of them.
global $wpdb;
$result = $wpdb->replace(
$wpdb->postmeta,
array(
'post_id' => $post->ID,
'meta_key' => 'ratingpost',
'meta_value' => $ratingvalue
),
array(
'%s',
'%s',
'%s'
)
);
I'd like to make this insert in all the posts without having to load every single url. I suppose I should do that via functions.phpbut I haven't found a proper way to do it.
Yes, you can achieve this by putting this code into functions.php , but this will insert all post every time site loads, so add this once and remove after complete execution.
<?php
global $wpdb;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
$ratingvalue = get_post_meta($post->ID,'ratingpost',true);
$result = $wpdb->replace(
$wpdb->postmeta,
array(
'post_id' => $post->ID,
'meta_key' => 'ratingpost',
'meta_value' => $ratingvalue
),
array(
'%s',
'%s',
'%s'
)
);
<?php endforeach;
wp_reset_postdata();?>
Just use SQL for this:
global $wpdb;
$updated = $wpdb->query( UPDATE {$wpdb->postmeta} pm, {$wpdb->posts} p SET pm.meta_value = {$ratingvalue} WHERE pm.meta_key = 'ratingpost' AND p.ID = pm.post_id );
I'm trying to filter my posts to only show the ones with that have a custom value for the field "Model" while sorting the posts by another custom field called "Price."
Here's the function I'm using (not working):
<?php
global $query_string;
query_posts( $query_string . "&meta_value=Model&orderby=meta_value&meta_key=Price&order=ASC");
?>
This function only shows Models, yet doesn't sort the posts by Price. If i add &meta_value=Model after order=ASC it sorts by Price but shows all posts, and not just Models.
Have you looked at http://codex.wordpress.org/Class_Reference/WP_Query
Specifically this section:
Multiple Custom Field Handling:
Display posts from several custom field:
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query = new WP_Query( $args );
did you try and array?
$args = array(
'meta_value' => array('Model','Price')
);
query_posts($args);
I know this is an old question, but I needed the answer today and couldn't find it anywhere. I found the question and then created an answer (shown below):
<?php
$sql = "
SELECT ID, meta1.meta_value, meta2.meta_value from $wpdb->posts p
JOIN $wpdb->postmeta meta1 ON meta1.post_id = p.ID
JOIN $wpdb->postmeta meta2 ON meta2.post_id = p.ID
WHERE p.post_status = 'publish'
AND p.post_type = 'post'
AND meta1.meta_key = 'Model'
AND meta2.meta_key = 'Price'
ORDER BY meta2.meta_value DESC
";
$posts_with_meta = $wpdb->get_col($sql);
$my_query = new WP_Query();
foreach ($posts_with_meta as $p) {
$post = get_post(intval($p));
setup_postdata($post);
print_r($post);
}
?>