WordPress.org

Ready to get started?Download WordPress

Forums

Simple Fields
getting fields data of a fieldgroup outside the loop (6 posts)

  1. nellone
    Member
    Posted 1 year ago #

    I would create a search functionality in header.php that finds posts by simple fields values.

    I need a list of all field names and selectable values that are saved in a fieldgroup.

    i can't find a function that works outside the loop.
    this function simple_fields_fieldgroup('my_fieldgroup_slug') returns an empty array.

    this is an example image to how search box should be:Image

    http://wordpress.org/extend/plugins/simple-fields/

  2. Pär Thernström
    Member
    Plugin Author

    Posted 1 year ago #

    You can pass a post id as the second argument to simple_fields_fieldgroup(). See info here: http://simple-fields.com/documentation/api/getting-values/simple_fields_fieldgroup/

  3. nellone
    Member
    Posted 1 year ago #

    Is there not a specific post ID to pass, i need to find all posts matching the search query sent by the user.
    To create the search query i need to build a form with inputs named as simple fields names and with related values.

  4. Pär Thernström
    Member
    Plugin Author

    Posted 1 year ago #

    ah, ok. perhaps the method get_field_group() does what you want.

    // Get and display info about a field group
    global $sf;
    $my_field_group_id = 10;
    $field_group_info = $sf->get_field_group( $my_field_group_id );
    sf_d( $field_group_info , '$field_group_info' );

    all the info should be in the variable $field_group_info.

  5. nellone
    Member
    Posted 12 months ago #

    thank you a lot! it's all what i needed!
    If you could show me an example of query where i can find posts with simple fields ... it will be great!

    this works?

    query_posts(array('numberposts' => '10', 'post_type' => array('filmp', 'corp', 'post'), 'meta_key' => '_simple_fields_fieldGroupID_1_fieldID_3_numInSet_0', 'meta_value' => 'dropdown_num_3'));
  6. nellone
    Member
    Posted 12 months ago #

    I'm having some trouble with the query
    My post are intended to be of custom type only, so i used pre_get_posts hook to modify the main loop, then i use the query->set to filter posts

    add_action( 'pre_get_posts', 'filtri_di_ricerca');
    function filtri_di_ricerca( $query ) {
        if ( is_admin() || ! $query->is_main_query() )
            return;
    
        if (is_home())
    	{
    		$query->set('post_type', 'post_annunci');  
    
    		$meta_query = array('relation' => 'AND');
    
    		// Only check these form fields
    		$fields = array( 'annuncio_contratto', 'annuncio_categoria', 'annuncio_tipologia', 'annuncio_citta');
    
    		foreach( $fields as $field )
    		{
    			if( $_GET[$field] != '' )
    			{
    				// We have something to match, otherwise ignore the field...
    				$meta_query[] = array(
    					'key' => $field,
    					'value' => $_GET[$field],
    					'compare' => '='
    				);
    			}
    		}
    
    		$query->set('meta_query',$meta_query);
    
        }

    I have some problem obtaining the meta keys as _simple_fields_fieldGroupID_1_fieldID_3_numInSet_0 instead of $fields, because in the search form i use input fields slugs as input names, so my $_GET variabiles are like annuncio_contratto=dropdown_num_16

    I seen an example in your blog post where you can order by the field slug, is there a similar way to launch a query with a field_slug?
    Or get the meta_key giving the slug?
    I cannot figure out to use action_pre_get_posts_meta($query)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.