Support

Account

Home Forums Front-end Issues How to search ACF Post Object Type Field

Solving

How to search ACF Post Object Type Field

  • I am using ACF from years.
    I am a WordPress Developer.

    I am unable to search only Post Object Type field in my Front end search page.

    Here is My Query.

    
    $args = array(
        'post_type' => 'recipes',
        'post_status' => 'publish',
        'paged' => $paged,
    'meta_query' => array(
                    array(
                    'key' => 'cookies_products_used', //Object Type Field name
                    'value' => $ptype, // Is a Search value which i am trying to get in posts
                    'compare'   => 'LIKE',
                     )
                )
            );

    I found that Post Object Type Field shows data in Arrya. But I am too much confused how to call it Here.
    Need urget help

  • Replace ‘paged’ => $paged by ‘posts_per_page’ => -1,

    
    	$args = array(
    		'post_type' => 'recipes',
    		'post_status' => 'publish',
    		'posts_per_page' => -1,
    		'meta_query' => array(
    			array(
    				'key' => 'cookies_products_used',
    				'value' => $ptype,
    				'compare' => 'LIKE'
    			)
    		)
    	);
    
    // start get values you want
    	$the_query = new WP_Query($args);
    	if ($the_query->have_posts()) {
    		while ($the_query->have_posts()) {
    			$the_query->the_post();
    			
    			// for example
    			$cookies_products_used = get_field('cookies_products_used',get_the_ID());
    		}
    	}
  • No Dear, You misunderstood me.
    The Custom Field i added is not a simple text Field. It is POST OBJECT TYPE FIELD.
    https://prnt.sc/ijh5si
    Click to see image

    In This, We can add more than 1 Value to the POST.

    I want to get data by this. I dont want to get only This. I want to GET data by searching this Field.

    Any By The way, Your Code didn’t work

  • There isn’t any way to search a post object field by text that is located in the related post object. The post object field stores the post ID, or an array of post IDS for the related posts.

    If you want to be able to search content of the related post and return the post it is related to, then you will also need to store that data in custom fields of the posts that you want returned. That probably sounds confusing, because it is.

    Let’s say for example that you want to search all of the post titles of posts in the post object field.

    The first thing that you need to do is to store that information in the same post with the post object field.

    Please read the following article. This article deals with making a repeater field queryable, however, the same principles apply. You need to get the titles of the posts and store them in a way that they become usable in WP_Query(). https://acfextras.com/dont-query-repeaters/

  • John,
    Is there any way at all to search post objects? We have used post objects on hundreds of pages, and I’m just trying to search the pages (meta_query) by the post object selected. Is that not possible?

  • The meta_value of a post object field is either a Post ID or an array of Post IDs.

    If you have the Post ID that you are looking for in that field, then yes. See this page https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ under the array based values section. In this case you would use a LIKE query and search for '"'.$post_id.'"'

    if on the other hand you want to do a search that is something like

    => posts your are searching => post object field => some value in the post selected in the post object field

    then no, this is not possible. I supposed it would be with an extremely complicated MySQL query, but it is not possible using WP_Query().

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘How to search ACF Post Object Type Field’ is closed to new replies.