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

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

  • couldn't get the data – Sabbir Dec 10 '18 at 9:04
  • I am not sure what do you mean? – Greg Winiarski Dec 10 '18 at 10:29
  • 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. – Sabbir Dec 10 '18 at 10:37
  • Let us discussion on chat – Sabbir Dec 10 '18 at 10:37
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'
      ),
  ),
  );
  • can you use my types name and fields name to this query? – Sabbir Dec 10 '18 at 9:09
  • yes sure please provide your type – Tejas Gajjar Dec 10 '18 at 9:11
  • my post_type is product_name and custom field name is : brand_name – Sabbir Dec 10 '18 at 9:14
  • $title = get_the_title();$args = array( 'post_type' =>'product_name', 'meta_query' =>array( array( 'key' => 'brand_name ', 'value' => $title, ), ), ); – Tejas Gajjar Dec 10 '18 at 9:17
  • let me know this works or not – Tejas Gajjar Dec 10 '18 at 9:18
0

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
    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 Apr 23 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.