I want to get the data from advanced custom field using wp_query from another custom post type:
$title = get_the_title();
$the_query = new WP_Query( array(
'posts_per_page'=>9,
'post_type'=>'product_name',
'order' => 'ASC',
'brand_name' => $title, /*brand_name is my custom field name*/
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
I want to get the data from advanced custom field using wp_query from another custom post type:
$title = get_the_title();
$the_query = new WP_Query( array(
'posts_per_page'=>9,
'post_type'=>'product_name',
'order' => 'ASC',
'brand_name' => $title, /*brand_name is my custom field name*/
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
Share
Improve this question
asked Dec 10, 2018 at 8:39
user155636user155636
3 Answers
Reset to default 1ACF plugin stores data in the wp_postmeta like the default WordPress custom fields.
To get data from a custom field your code should look like this (assuming the custom field name is "brand_name").
$title = get_the_title();
$the_query = new WP_Query( array(
'posts_per_page'=>9,
'post_type'=>'product_name',
'order' => 'ASC',
'meta_query' => array(
array( "key" => "brand_name", "value" => $title )
),
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
For more details on using meta fields in the WP_Query please see https://codex.wordpress/Class_Reference/WP_Query#Custom_Field_Parameters
You have to use the ACF API get_field()
function.
The get_field()
function requires the name of the field. If you are using the loop then that's all you need. If you are outside the loop you will need to provide the post ID as the second argument. The final, optional argument is boolean and determines whether formatting is applied to the data, this defaults to true so if you don't want formatting you need to provide a false here.
$val = get_field('frequency', $postID, false);
Where 'frequency'
is the name of the custom field you desire.
Since this method only requires that you are either in the loop or that you know the post ID you can use in any of the WordPress database methods that are available to you.
You have to use meta query like that name of filed in key please follow this example and let me know if this helps or now
$args = array(
'post_type' =>'post_type',
'paged' => $page,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => '386',
'field' => 'term_id',
)),
'meta_query' =>array(
array(
'key' => 'textbox_4',
'value' => $cond,
'compare' => 'LIKE'
),
),
);