• Resolved buckindb

    (@buckindb)


    Is it possible to query/filter by a custom field?

    I have added a custom checkbox group to my directory. I would like to display a list of connections that have a specific value selected. Or, alternatively, group the connections by each option in the checkbox group.

    For example, consider the following checkbox group options:
    Option A
    Option B
    Option C

    John Doe has Option A and Option B selected.
    Jane Doe has Option B selected.
    Robert Smith has Option C selected.

    Ultimately, what I am trying to display is the following:

    Option A
    – John Doe

    Option B
    – Jane Doe
    – John Doe

    Option C
    – Robert Smith

    I’m happy to build the Option sections on my page and use multiple shortcodes for each section if necessary.

    Thoughts?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Steven

    (@shazahm1hotmailcom)

    You might be able to utilize the meta_query [connections] shortcode option:

    Can you share the code snippet for your checkboxes so I can confirm?

    Thread Starter buckindb

    (@buckindb)

    Here is the snippet from my functions.php:

    add_action( 'cn_metabox', 'cn_register_merit_badge_metabox' );
     
    function cn_register_merit_badge_metabox() {
    
       $atts = array(
    		'title'    => 'Merit Badge Counselor',
    		'id'       => 'merit_badge_counselor',
    		'context'  => 'normal',
    		'priority' => 'core',
    		'fields'   => array(
    			array(
                   'name'        => 'Merit Badges',    // Change this field name to something which applies to you project.
                   'show_label'  => TRUE,             	// Whether or not to display the 'name'. Changing it to false will suppress the name.
                   'id'          => 'merit_badges', 	// Change this so it is unique to you project. Each field id MUST be unique.
                   'type'        => 'checkboxgroup',  	// This is the field type being added.
                   'options'     => Merit_Badge::merit_badge_names(),
                   'default'     => '',                // This is the default selected option. Leave blank for none.
                ),
    		),
    	);
    
    	cnMetaboxAPI::add( $atts );
    }

    I have a static class Merit_Badge, that maintains a list of merit badges for our scout troop. The static method, returns an associative array of merit badge ids and names.

    Thanks!

    Plugin Author Steven

    (@shazahm1hotmailcom)

    Ok, this should work, but you will have to make a slight change…

    Checkbox groups are saved as entry meta as a serialized array where the array values are the option keys. In this case, the merit badge ids.

    What you should do is change the field type checkbox:

    Iterate thru the Merit_Badge::merit_badge_names() array to build the fields array as shown in the doc.

    Doing this will ensure that each meta merit badge id is stored separately per entry. Then you should be able to use the meta_query following the OR example.

    I hope this helps; please let me know.

    Plugin Author Steven

    (@shazahm1hotmailcom)

    This ticket is being marked resolved due to inactivity. If you still need help with this, please do not hesitate to let me know.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Query/filter by custom field?’ is closed to new replies.