0

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) 
);

3 Answers 3

1

ACF 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.org/Class_Reference/WP_Query#Custom_Field_Parameters

4
  • couldn't get the data
    – user155636
    Commented Dec 10, 2018 at 9:04
  • I am not sure what do you mean? Commented Dec 10, 2018 at 10:29
  • 1
    this is my single-{custom-post-type}.php page I wanna get the data using this pages title. this title is from another custom post pages field name. So if my another custom post field name is matched to this title then get that data.
    – user155636
    Commented Dec 10, 2018 at 10:37
  • Let us discussion on chat
    – user155636
    Commented Dec 10, 2018 at 10:37
1

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.

1
  • 1
    Thanks for the edits @Sven. I only capitalized "Loop" because that is how it is specifically referred to in the WordPress documentation. The Loop. Thanks for your assistance.
    – hyjinx
    Commented Apr 23, 2019 at 15:43
0

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'
      ),
  ),
  );
5
  • can you use my types name and fields name to this query?
    – user155636
    Commented Dec 10, 2018 at 9:09
  • yes sure please provide your type Commented Dec 10, 2018 at 9:11
  • my post_type is product_name and custom field name is : brand_name
    – user155636
    Commented Dec 10, 2018 at 9:14
  • $title = get_the_title();$args = array( 'post_type' =>'product_name', 'meta_query' =>array( array( 'key' => 'brand_name ', 'value' => $title, ), ), ); Commented Dec 10, 2018 at 9:17
  • let me know this works or not Commented Dec 10, 2018 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.