Support

Account

Home Forums General Issues Get a field object outside of the post context

Solved

Get a field object outside of the post context

  • I have a select field called ‘language’.

    I am creating a search page where i want to display all languages available as checkboxes.

    How can I retrieve the field object ‘language’ without passing the post id as parameter (i am on the search page, so no post id)?

    I cannot find a function to list the field choices outside of the post scope.

    thanks

  • I’m not sure I completely understand. Is the “language” field part of each post?

    Are you trying to show these language buttons for each post in the search results or something else?

  • language field is only for some custom post types, but if i must put it on all post types, i could do it.

    I just want to output all available select options that are part of this field (french, english, chinese, spanish, etc…), regardless of posts in search results.

    I just want to show the field choices on a page (the custom field is not assigned to the search page so i cannot use get_field_object(‘language’, $post->ID) ).

  • I still may not be clear on where this field is, but there is a loop on the search results page that shows the results of the search, or there should be.

    
    if (have_posts()) {
      while (have_posts()) {
        the_post();
        // this will get the field object on the current posts
         get_field_object('language');
      }
    }
    

    If you’re trying to show the entire search results in different languages, I’m not sure I can be much help with the displaying of languages but I’d create an options page where I could add option values for pages that do not have an editor. Then I’d get that option value and use it instead of a field on a particular post.

  • outside of the search result loop, i have the search form like this :

    Sort by :
    – post type
    – post_type_1_checkbox
    – post_type_2_checkbox
    – …
    – date
    – 2001
    – 2003
    – …
    – language
    – french
    – english
    – ….

    the languages choices must sort all the languages select option on the field ‘language’. That is what i want 🙂

    because i am not on a post that is using this field (i am on the search page), i cannot use
    get_field_object('language')['choices'][get_field('language')]
    to sort all options available for ‘language’ field.

    get_field_object(‘language’) returns false because i am not on the scope of that field (this field is for posts not for pages).

    I want something to get the field structure regardless of the post id.
    Something like the export of acf fields, but with a function i can use on my theme/plugin

  • I think I have it. So, what you basically want to do it get all the languages currently available for any post, meaning all of the post on the site so that you can search by that value?

    Are you trying to search all post types or just a certain post type?

  • yes that’s it.

    Because I have an option to sort by post type in my search form, i would like to be able to search for all post OR just certain post types.

    if possible i don’t want to make a wp query just to get the field options, i want to get the field options like on the export tools on acf admin page (i don’t care about all that, but it is just an example of sorting the field structure without a $post->ID) :

    if( function_exists(‘acf_add_local_field_group’) ):

    acf_add_local_field_group(array (
    	'key' => 'group_55ba39c6cc9d7',
    	'title' => 'Langues',
    	'fields' => array (
    		array (
    			'key' => 'field_55ba39cd8bb11',
    			'label' => 'Langue',
    			'name' => 'langue',
    			'type' => 'select',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array (
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'choices' => array (
    				'francais' => 'Français',
    				'anglais' => 'Anglais',
    				'espagnol' => 'Espagnol',
    				'allemand' => 'Allemand',
    				'portugais' => 'Portugais',
    				'russe' => 'Russe',
    				'arabe' => 'Arabe',
    				'chinois' => 'Chinois',
    				'hindi' => 'Hindi',
    				'japonais' => 'Japonais',
    				'autre' => 'Autre',
    			),
    			'default_value' => array (
    				'francais' => 'francais',
    			),
    			'allow_null' => 1,
    			'multiple' => 0,
    			'ui' => 0,
    			'ajax' => 0,
    			'placeholder' => '',
    			'disabled' => 0,
    			'readonly' => 0,
    		),
    	),
    	'location' => array (
    		array (
    			array (
    				'param' => 'post_type',
    				'operator' => '==',
    				'value' => 'document',
    			),
    		),
    	),
    	'menu_order' => 1,
    	'position' => 'acf_after_title',
    	'style' => 'default',
    	'label_placement' => 'top',
    	'instruction_placement' => 'label',
    	'hide_on_screen' => '',
    	'active' => 1,
    	'description' => '',
    ));
    
    endif;
  • My suggestion would be to go straight to the database to get the values with a custom query to get the available languages. The reason why is that the only way you’re going to populate that list otherwise would be to do a second WP_Query to get every post on your site then loop through them and get the language value for every post and build an array of values.

    
    $query = 'SELECT DISTINCT meta_value
              FROM ' . $wpdb->postmeta.'
              WHERE meta_key = "language"
              ORDER BY meta_value';
    $results = $wbdb->get_col($query);
    

    That should return an array of languages set for all posts on your site.

    I’m not using prepare above because there are no user inputted values and the query should be safe.

  • thanks i’ll try that!
    anyway it would be cool to be able to do that with an acf function like

    get_field_object( 'field_name', $post_id = false );

    If the post id is false, it should return the field object without having to pass a real post id as an argument.


    @Elliot
    if you read this…

  • The problem is mainly that what you’re looking for is all the custom field values for a specific custom field for all the posts on the site. This is problematic using any of the standard WP functions. There is nothing in WP that will return this information without using wpdb directly.

    My solution is pretty quick and a bit dirty, but should work in most cases and be safe to use. For example, I’m not using a JOIN on the posts table, so my query will return values set for any type of post, including drafts and trash or whatever but 99% of the time that should not matter, unless you’ve recently trashed all of the posts of a language or just started creating drafts of a new language. I’m not really an expert on wpdb

  • i don’t have the same approach, it should be an acf function not a wp function.
    ACF should give a function to output $field[‘choices’] regardless of where it is used and regardless of if it is used or not.

    if the plugin can export a field, we should be able to get the raw field structure with an acf function, outside of the post scope 🙂

  • I thought that you only wanted to show the actual values that were used, not all the choices. Sorry, I may have given you a solution that was not what you were looking for.

    If you want to do this you can do it with ACF, but like you said in the first post you need to supply a post ID. This can be overcome by using the ID of the first post in the results.

    
    get_field_object('language', $wp_query->posts[0]->ID);
    

    But this will only work if there are results to be shown.

    It can also be done using the field key instead of the field name. This is explained in the documentation for get_field_object() http://www.advancedcustomfields.com/resources/get_field_object/

    
    get_field_object('field_1234567890123');
    
  • Hey John, any workaround for v5? This get_field_object('key'); does not work anymore globally, just for the current or latest post.

  • I’ve discovered there’s this built-in function:

    acf_get_field('your_field_id')

    This seems to get the custom field outside of any post context.

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

The topic ‘Get a field object outside of the post context’ is closed to new replies.