ACF Relationship Fields - get_field values โ€‹โ€‹from another message type

On my POSTS (regular message type) page, I set the ACF relationship field. Inside this, I can select the name of the company, which are under the type post_listings.

Now I have the following code on the directory listing page, and so using just get_field does not work, because these values โ€‹โ€‹are not on this page, they are instead in a different place like POST.

So unsure how to capture information.

The code on one of the pages, which is under the type DIRECTORY_LISTINGS:

$posts = get_field('related_articles'); if( $posts ): ?> <ul> <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?> <?php setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endforeach; ?> </ul> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?> <?php endif; ?> 

An example diagram, as I am not very good at explaining with text. enter image description here

Currently, I have set the relationship field on the company editing page (directory_listing). It works when performing the following actions: 1) Related entries in this account โ†’ select post โ†’ publish โ†’ now displays a list on the business listing page. Example here: http://bit.ly/1vwydDl (bottom of page)

2) I want to select the business from which the posts will be displayed from the POST edit page. I can put the field there through ACF without problems, but get it to actually display the results, which I cannot understand.

+5
source share
2 answers

Background Information:

get_field () has three parameters:

  • $field_name : the name of the field to retrieve. e.g. "page_content" (required)
  • $post_id : The specific identifier of the message in which your value was entered. By default, the current message identifier is used (not required). It can also be parameters / taxonomies / users / etc
  • $format_value

If you were only interested in grabbing a specific record (from which you knew the identifier), the key would be the second parameter ( $post_id ). There is nothing magical about ACF. Quite simply: meta_value (i.e. the directory in which the message is placed is attached) is stored in each message ( $post_id attached to this message).

However, in your case, we do not know the identifier of the messages we want to receive.

Decision:

If we explain what you want to do in a simple sentence, this sentence will look something like this:

Show / receive messages on a directory_listings page (a custom message type) that has a meta_value that points to this page.

Obviously, you cannot use get_field() because your problem has nothing to do with getting the field. Rather, you need to "find messages that have a specific field." ACF has excellent documentation on this .

Luckily for you, WordPress comes with an awesome class called WP_Query , and a similarly awesome function is called get_posts () . Therefore, looking back at our suggestion above and translating it into a function, we want: get_posts() , where meta_key has the value current $post_id .

Or, more specifically, on the directory_listings page, you will receive the following request:

 $related_articles = get_posts(array( 'post_type' => 'post', 'meta_query' => array( array( 'key' => 'related_articles', // name of custom field 'value' => '"' . get_the_ID() . '"', 'compare' => 'LIKE' ) ) )); if( $related_articles ): foreach( $related_articles as $article ): // Do something to display the articles. Each article is a WP_Post object. // Example: echo $article->post_title; // The post title echo $article->post_excerpt; // The excerpt echo get_the_post_thumbnail( $article->ID ); // The thumbnail endforeach; endif; 
+10
source

If this is the custom field you are looking for inside this query, you can do it like this:

 <?php $posts = get_field('related_articles'); if( $posts ): ?> <ul> <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?> <?php setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php the_field('your_custom_field',$post); ?> </li> <?php endforeach; ?> </ul> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?> <?php endif; ?> 
0
source

Source: https://habr.com/ru/post/1208737/


All Articles