fetch customer field on wp_posts from wordpress - wordpress

I am using wordpress, I have added "city_name" to wp_posts, now i am using the following query but did not getting result according to city_name.
$query_args = array(
'post_type' => 'product',
'order' => 'DESC',
'post_status' => 'publish',
'city_name' => 'karachi',
'showposts' => 7
//'offset'=> 0,
//'posts_per_page'=>2
);

store city name in meta field using update_post_meta wordpress function
update_post_meta('post id here','city_name','karachi')
and now you can query for this field like so :
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'city_name',
'value' => 'karachi'
)
)
);
$query = new WP_Query($args);
Refer Here : Wp Query

Related

How to sort WP Query by meta key exist or that meta value is equal one

I have filter page which is contains all the product list, And i have two option first once is latest another one is featured. In database the post meta storing as is_feature = 1. During wp_query its only fetching is feautre =1. Actually i need data as like is_feature = 1 from starting ,which post doesn't have is_feature = '' or doesn't is_feature meta key, they need to display later is_feature = 1.
This is my query:
$conditions = array(
'posts_per_page' => 9,
'paged' => $paged,
'post_type' => 'custom',
'meta_key' => 'is_feature',
'order_by' => 'meta_key=is_feature',
'order' => 'DESC',
'post_status' => "publish", )
$the_query = new WP_Query( $conditions );
Replace order_by to orderby.
Use meta_value as orderby parameter:
$conditions = array(
'posts_per_page' => 9,
'paged' => $paged,
'post_type' => 'custom',
'meta_key' => 'is_feature',
'orderby' => 'meta_value',
'order' => 'DESC',
'post_status' => "publish"
);
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

Use 'LIKE %word%' for ACF Query Wordpress

I use ACF plugin on my wordpress site and I want to code custom research.
I have many products and I want to display products which contain the searched word.
I do my query like this :
$args = array(
'post_type' => 'product',
'meta_key' => 'brand',
'meta_value' => $word,
'compare' => 'LIKE');
$the_query = new WP_Query($args);
But this display only products with the brand which exactly matches with $word.
for exemple if I search "yan" I want to display products with the brand "YANMAR", "POLYAN", "TRYANPO", etc.
How to do this please ?
Thank you and have a good day !
Try below code.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'brand',
'value' => $word,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query($args);

How to query specific custom post with custom fields in a post_type?

I'm new into wordpress and kinda confused how to get 1 post in a custom post. I only knew the loop where it echo all the content in a post_type.
I'm aiming to get 1 post from a post_type 'product-category' and the meta_key is 'product-category-and-type'
You could try this query for custom in wordpress:
$args = array(
'post_type' => 'product-category',
'post_status' => 'publish',
"numberposts" => 1,
'meta_query' => array(
array(
'key' => 'product-category-and-type',
'value' => 'meta_value'
)
)
);
$getPosts = new WP_Query($args);
Just copy and paste below mentioned code at your desired position and replace "your_meta_value" with your actual meta value for meta key "product-category-and-type".
You will get your expected result :
<?php $args = array(
'post_type' => 'product-category',
"numberposts" => 1,
'post_status' => 'publish',
'meta_query' => array(array('key' => 'product-category-and-type','value' => 'your_meta_value'))
);
$myposts = new WP_Query($args);
while($myposts->have_posts()) : $myposts->the_post();
the_title();
endwhile;?>
$args = array(
'post_type' => 'product-category',
'post_status' => 'publish',
'posts_per_page' => '1',
'tax_query' => array(
array(
'taxonomy' => 'product-category-and-type',
'field' => 'slug'
),
),
);
$result = new WP_Query($args);

WP_Query with “post_title LIKE 'something%'” and category

I need to do a WP_Query with a LIKE on the post_title and category
This query_post is not working
query_posts(
array(
'post_type' => 'add_buying',
'like' => $keywords,
'posts_per_page' => 5,
'taxonomy' => 'add_country',
'term' => 'drawing'
));
Check this url and change the like parameter.
query_posts( array(
'post_type' => 'add_buying',
's' => $keywords,
'posts_per_page' => 5,
'taxonomy' => 'add_country',
'term' => 'drawing'
));
Change your second parameter to 's' and I think it's going to work:
$args = array(
'post_type' => 'post',
's' => $search_term,
'post_status' => 'publish'
);
$wp_query = new WP_Query($args);
And good luck
title (string) - use post title (available with Version 4.4).
$args = array(
'post_type' => 'post',
'title' => $title,
'post_status' => 'publish'
);
$wp_query = new WP_Query($args);

Wordpress Query on multiple post types

I am completely stumped on this. The code below allows me to query multiple post types. I break them down like this because of the use of categories. The weird thing is, I only get posts from the post_type = 'post'. The final query I use post__in to establish the posts that I want by ID. If I print out $post_ids, I get the exact IDs that I am looking for. But my final query doesn't give me those IDs. Thoughts??
$postArgs = array(
'post_type' => 'post',
'cat' => '16,17,18',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$videoArgs = array(
'post_type' => 'occ-videos',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$photoArgs = array(
'post_type' => 'occ-photography',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$docArgs = array(
'post_type' => 'wpfb_filepage',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$posts_query = get_posts($postArgs);
$docs_query = get_posts($docArgs);
$video_query = get_posts($videoArgs);
$photo_query = get_posts($photoArgs);
// start putting the contents in the new object
$all_posts = array_merge($posts_query, $docs_query, $video_query, $photo_query);
$post_ids = wp_list_pluck( $all_posts, 'ID' );//Just get IDs from post objects
print_r($post_ids);
$artArgs = array(
'posts_per_page' => 20,
'post_status' => 'publish',
'orderby' => 'post__in',
'post__in' => $post_ids);
$artQuery = get_posts($artArgs);
My understanding is that Wordpress always defaults to the post_type of post. So it's only finding posts that have one of those IDs – and ignoring your custom post types.
Trying adding a line to to your $artArgs
$artArgs = array(
'post_type' => array('post','page','occ-videos','occ-photography'), //Add this line
'posts_per_page' => 20,
'post_status' => 'publish',
'orderby' => 'post__in',
'post__in' => $post_ids
);
And add whatever post types you need Wordpress to query.

Resources