• Hello all. Thanks for taking a look at my question.

    What I have…

    A front end submission form that allows members to submit posts for a custom post type: ‘game-previews’. I have several custom_meta and custom taxonomies on the form. All is fine, except one instance.

    Where I’m stuck…

    I have one custom taxonomy where I don’t want members to just randomly type new terms (or guess existing ones..lol). The submitter will also need to be able to select multiple taxonomies if they choose. This taxonomy is called ‘game_platforms’. I know how to display the existing taxonomy values in a drop-down by using…

    <fieldset>
    <div class="platform-type-input">
      <h3>What Type of Platform Is It?</h3>
      <?php $platformtypes = get_terms('game_platforms', 'hide_empty=0');  ?>
      <select name='game_platforms' id='game_platforms' style="width: 95%" class="required">
    <!-- Display taxonomies as options -->
    <?php $names = wp_get_object_terms($post->ID, 'game_platforms'); ?><option class='game_platforms' value=''
    <?php if (!count($names)) echo "selected";?>>Please Choose A Platform Type</option>											        <?php foreach ($platformtypes as $game_platforms) {
    if (!is_wp_error($names) && !empty($names) && !strcmp($game_platforms->slug, $names[0]->slug))
    echo "<option class='game_platforms' value='" . $game_platforms->slug . "' selected>" . $game_platforms->name . "</option>\n";
    else
    echo "<option class='game_platforms' value='" . $game_platforms->slug . "'>" . $game_platforms->name . "</option>\n";
    } ?>
    </select>
    </div>
    </fieldset>

    This I’ve used in another instance for another situation on the project, but in this case, I can’t seem to figure out how to fetch & display the existing taxonomy values in a checkbox format for the form.

    Any thoughts, guidance, tips, snippets would be greatly appreciated. Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter VegasKev88

    (@vegaskev)

    Progress….almost there.

    So I was able to pull the existing values from the taxonomy using this…

    <fieldset>
    	<h4>Testing The TaxBoxes</h4>
    <?php $platformsTerms = get_terms('game_platforms',array( 'taxonomy' => 'game_platforms' ));
    foreach($platformsTerms as $term){
        $checked = (has_term($term->slug, 'game_platforms', $post->ID)) ? 'checked="checked"' : '';
        echo "<label for='term-" . $term->slug . "'>" . $term->name . "</label>";
        echo "<input type='checkbox' name='term" . $term->slug . "' value='" . $term->name . "' $checked />";
    } ?>
    
    </fieldset>

    …now the only issue is that it only pulls values that have been used previously in an existing post. The problem with this is that it’s a new site with zero posts, so once a user would not be able to submit any values for this specific taxonomy as none have been used before (even though they were created manually in wp admin.

    I’m almost there…just need that last bit…Murphy’s Law

    Moderator bcworkz

    (@bcworkz)

    You should be able to alter the query used by get_terms() to return all terms regardless if they are used in a post or not. Hook the filter ‘terms_clauses’ and examine the clauses passed for the part that is limiting the query. It will probably help to see how the clauses are used in an actual query. Review the source code for get_terms() in wp-includes/taxonomy.php.

    You can use this filter to alter the offending clause in order to get the results you want. At least it appears so. I have not done this myself, but it does appear to be the first thing to try.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Retrieve Existing Taxonomy Values For Form As Checkbox’ is closed to new replies.