WP Front End Form Not Saving My Custom Taxonomies - wordpress

I am trying to create a front end posting form through wordpress that will allow users to create a post when schools are closed. I have 4 custom taxonomies closed_schools, study_open, safe_arrival, hour_delay. each has the same list of 3o or so schools. The problem is that I can't display which schools have been selected under there categories after the post is created. If I build the post in the back end it works, just not from the front end, SO I know there is something wrong with how I am storing the taxonomies. Any suggestion are very welcomed, thanks
My Front End Form Template
if(isset($_POST['submit'])){
$title = $_POST['title'];
$description = $_POST['description'];
$category = $_POST['cat'];
$tags = $_POST['post_tags'];
$terms = $_POST['terms'];
$error_msg = array();
if($title == ''){
$error_msg[] = 'Please select an area and/or region.';
}
if($description == ''){
$error_msg[] = 'Please write a description for the closure and/or delay.';
}
if($category == '-1'){
$error_msg[] = 'Please identify the reason for the closure and/or delay.';
}
else if( !$error_msg && 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post"){
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => array($_POST['cat']), // Usable for custom taxonomies too
'tags_input' => array($tags),
'tax_input' => array(
'closed_schools' => $_POST['terms'],
'study_open' => $_POST['terms'],
'safe_arrival' => $_POST['terms'],
'hour_delay' => $_POST['terms'],
),
'post_status' => 'publish', // Choose: publish, preview, future, draft, etc.
'post_type' => 'post', //'post',page' or use a custom post type if you want to
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
//SET OUR TAGS UP PROPERLY
wp_set_post_tags($pid, $_POST['post_tags']);
//SET OUR POST TERMS, CUSTOM TAXONOMIES
wp_set_post_terms($pid, $_POST['terms'], 'closed_schools', 'safe_open', 'safe_arrival', 'hour_delay', true);
//REDIRECT TO THE NEW POST ON SAVEs
$link = get_permalink( $pid );
wp_redirect( $link );
//POST THE POST
do_action('wp_insert_post', 'wp_insert_post');
}
}
Inside the Form I display the schools in check boxes from the 4 taxonomies like so. I just change the names out for each taxonomy I am showing.
<fieldset>
<label for="closed_schools" class="selection-title">Closed Schools:</label>
<?php
$closed_schools = get_terms('closed_schools', 'orderby=id&hide_empty=0');
$counter = 0;
foreach ($closed_schools as $close) {
$counter++;
$option = '<label for="'.$close->slug.'">'.$close->name.'</label>';
$option .= '<input type="checkbox" name="terms[]" id="'.$close->slug.'" value="'.$close->slug.'">';
echo $option;
}
?>
</fieldset>
Finally I show output the terms on loop.php and single.php
<?php echo get_the_term_list( $post->ID, 'closed_schools', '<strong>Closed Schools:</strong> ', ', ', '' ); ?>
<?php echo get_the_term_list( $post->ID, 'study_open', '<strong>Open For Study Purposes Only:</strong> ', ', ', '' ); ?>
<?php echo get_the_term_list( $post->ID, 'safe_arrival', '<strong>Open for Students Who Can Ariive Safely:</strong> ', ', ', '' ); ?>
<?php echo get_the_term_list( $post->ID, 'hour_delay', '<strong>2 Hour Delay:</strong> ', ', ', '' ); ?>

I figured it out! Here is my answer.
I needed to use wp_set_object_terms instead of wp_set_post_terms for each and it worked
//SET OUR POST TERMS, CUSTOM TAXONOMIES
wp_set_object_terms($pid, $_POST['terms'], 'closed_schools');
wp_set_object_terms($pid, $_POST['terms'], 'study_open');
wp_set_object_terms($pid, $_POST['terms'], 'safe_arrival');
wp_set_object_terms($pid, $_POST['terms'], 'hour_delay');

Related

Shortcode display featured image and permalink from pages in Wordpress

I am trying to make a shortcode that can display featured image and permalink from chosen pages on my front-page in Wordpress.
It works fine to display one page, but I want to be able to choose different pages. I cannot figure out how....
// Creating Shortcodes with parameter to display pages
function my_shortcode_display_pages($attr, $content = null){
//call global variable
global $post;
// Defines Shortcode's Attributes
$shortcode_args = shortcode_atts(
array(
'type' => '',
'pagename' => '',
'num' => '',
'order' => ''
), $attr);
// set an array with query arguments
$args = array(
'post_type' => $shortcode_args['type'],
'pagename' => $shortcode_args['pagename'],
'posts_per_page' => $shortcode_args['num'],
'order' => $shortcode_args['order']
);
// get posts
$recent_posts = get_posts($args);
$output = '<div class="cards">';
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$output .= '<div class="card">' . ''.get_the_post_thumbnail() .'<h2 class="entry-title-index">'.get_the_title() .'</h2>'.'</div>';
endwhile;
wp_reset_postdata();
endif;
//return the output.
return $output . '</div>';
}
// Register the shortcode.
add_shortcode( 'my_display_pages', 'my_shortcode_display_pages' );
[my_display_pages pagename="hotell"]

Save Selected Option in Meta Box

i am going to build my own theme
I try to add select to post_type meta box, but every update post my select not on selected option but show the first option (blank value)
here is my code
function mhs_data() {
global $post;
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
$nisn = get_post_meta($post->ID, '_nisn', true);
$rel = get_post_meta($post->ID, '_rel', true);
}
echo '<p>NISN</p>';
echo '<input type="text" name="_nisn" value="' . $nisn . '" class="widefat" />';
echo '<p>Relationship</p>'; ?>
<select name="_rel" id="_rel">
<option value="">Relationship</option>
<option value="Single" <?php selected( $rel, 'Single' ); ?>>Single</option>
<option value="Marry" <?php selected( $rel, 'Marry' ); ?>>Marry</option>
</select>
<?php
}
function mhs_data_meta($post_id, $post) {
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
$events_meta['_nisn'] = $_POST['_nisn'];
$events_meta['_rel'] = $_POST['_rel'];
foreach ($events_meta as $key => $value) {
if( $post->post_type == 'revision' ) return;
$value = implode(',', (array)$value);
if(get_post_meta($post->ID, $key, FALSE)) {
update_post_meta($post->ID, $key, $value);
} else {
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key);
}
}
add_action('save_post', 'mhs_data_meta', 1, 2);
Please help me to correct my code
Using Codestar Framework its very simple tu add metaboxes to your custom post, create configuration page, plugins, taxonomies and use the customizer if you prefer use it instead of a configuration page. see the documentation here for more information.
in your example, to add a select metabox shuld use a code similar to:
load the framework on your functions.php
require_once __DIR__ . '/'.$FRAMEWORK_PATH.'/cs-framework/cs-framework.php';
edit the cs-framekork.php configuration file to active only the features you need:
defined('CS_ACTIVE_FRAMEWORK') or define('CS_ACTIVE_FRAMEWORK', false); // if you need it for plugin or configuration page
defined('CS_ACTIVE_METABOX') or define('CS_ACTIVE_METABOX', true); // if you only need it for metabox
defined('CS_ACTIVE_TAXONOMY') or define('CS_ACTIVE_TAXONOMY', false);
defined('CS_ACTIVE_SHORTCODE') or define('CS_ACTIVE_SHORTCODE', false);
defined('CS_ACTIVE_CUSTOMIZE') or define('CS_ACTIVE_CUSTOMIZE', false);
and then in your function.php or as I prefer on another file included in your function.php register your metabox
function register_this_metabox($options)
{
$options = array(); // this will clean the default cs-framework configuration
$options[] = array(
'id' => 'the_metabox',
'title' => 'Meta Box title',
'post_type' => 'post', // the post type where the metabox appears
'context' => 'side', // metabox position
'priority' => 'high',
'sections' => array( // metabox fields, see the documentation
array(
'name' => 'the_metabox_fields',
'fields' => array(
array(
'id' => 'the_metabox_select_field',
'type' => 'select',
'title' => 'Select Field',
'options' => array(
'opt1' => 'Option 1',
'opt2' => 'Option 2',
'opt3' => 'Option 3',
),
'default_option' => 'Select a option',
),
),
),
),
);
return $options;
}
add_filter('cs_metabox_options', 'register_this_metabox');
And now you only have to get the values in your theme:
$post_metabox = get_post_meta($post->ID, 'the_metabox', true);
$selected_option = $post_metabox['the_metabox_select_field']

Get featured images from custom taxonomy terms

Oke, first of I downloaded this plugin. This enables me to give my custom taxonomy terms an image. This works great. But the way my theme is setup and the way the plugin wants to work, isn't for me.
I only want the images to show in the place where I say it should. That's why I used terms (categories). Because then I can use this code:
<?php
//list terms in a given taxonomy
$taxonomy = 'our_team';
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?>
</ul>
That only shows the names of the team members when you activated them (checked the box) in the post, and not in other places.
Now I would like to be able to use a similar code but for the featured images. The plugin uses this code to get the thumbnails.
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="my-custom-class-name">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'post_id' => 15,
'taxonomy' => 'our_team',
) );
Does anyone know how to "merge" these two codes together and generate an image only if I checked the box in the custom post type. Or knows another way to do this?
** UPDATE **
Ok, getting somewhere. The code is now only displayed where I want it. With the help of Rohit Kishore! Thanks! Here is the working code:
<div class="teamleden over-ons-ul">
<?php $terms = get_the_terms( $post->ID , 'our_team' );
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="our-team">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'taxonomy' => 'our_team',
) );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'our_team' );
if( is_wp_error( $term_link ) )
continue;
}
?>
</div>
The print code had to be before the foreach part...

Custom Wp_Query still uses the main post id

On my Wordpress website, I have the following code:
if ( is_page( 8 ) ) {
include_once('somecustompage.php');
}
And in my somecustompage.php, I use a custom WP_Query:
// WP_Query arguments
$args = array (
'post_type' => 'farewell',
'post_status' => 'publish',
'pagination' => true,
'posts_per_page' => '10',
'sticky' => false
);
// The Query
$current_farewell_query = new WP_Query( $args );
// The Loop
if ( $current_farewell_query->have_posts() ) {
$current_farewells = '<table><tr><th>NAME</th><th>LOCATION</th><th>FAREWELL</th></tr>';
while ( $current_farewell_query->have_posts() ) {
$farewell_name = ( get_field('name') ? get_field('name') : '' );
$chapels = wp_get_post_terms( get_the_ID(), 'chapels', array('fields' => 'name') );
$current_farewell_query->the_post();
$current_farewells .= '<tr>';
$current_farewells .= '<td>' . $farewell_name . '</td>';
$current_farewells .= '<td></td>';
$current_farewells .= '<td></td>';
$current_farewells .= '</tr>';
}
$current_farewells .= '</table>';
} else {
$current_farewells = 'No Current Farewells.';
}
// Restore original Post Data
wp_reset_postdata();
However, when I try to use the post IDs within this custom loop, both get_the_ID() and $post->ID return 8, which is the main page post id. I need to use the new post IDs that are returned in the new query.
Anybody knows how to resolve this?
I found where the problem was. Everything should've been called after this line: $current_farewell_query->the_post(); for the query to work as expected.

wp how to exclude filter from the custom query

I am trying to exclude a filter (slug) from a loop. I am researched exclude and have tried it in various places in the code. Usually I break the page. This is the code, I am attempting to change to exclude the slug 20. It is a filter named 'american'. I have tried to exclude at the beginning of the array, didn't work; then, I tried after the foreach($catz as $cat) section. I tried this ‘exclude=20&title_li=' . I tried cat=-20 and various other combinations. Any help would be very very appreciated.
// The Custom Query
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => $counter_folio,
'paged' => $paged,
'order' => 'DESC'
);
query_posts( $args );
while( have_posts() ) : the_post();
$color = substr(get_option('dcb_dynamic_color'), 1);
// Get the original thumbnail of the post
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), false, '' );
$excerpt = get_the_excerpt();
// Get the custom fields
$vimeo = get_post_meta($post->ID, "vimeo", true);
$youtube = get_post_meta($post->ID, "youtube", true);
// Get the filter > Category of item
$catz = wp_get_object_terms($post->ID,'filters');
foreach($catz as $cat){
$currcat = $cat->slug;
$catname = $cat->name;
break;
}
$counter++;
?>
If you want to filter the post with id 20 simple exclude it with the following code
// The Custom Query
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => $counter_folio,
'paged' => $paged,
'order' => 'DESC'
);
query_posts( $args );
while( have_posts() ) : the_post();
$color = substr(get_option('dcb_dynamic_color'), 1);
// Get the original thumbnail of the post
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), false, '' );
$excerpt = get_the_excerpt();
// Get the custom fields
$vimeo = get_post_meta($post->ID, "vimeo", true);
$youtube = get_post_meta($post->ID, "youtube", true);
// Get the filter > Category of item
$catz = wp_get_object_terms($post->ID,'filters');
foreach($catz as $cat){
if($cat->slug != 'american')
{
$currcat = $cat->slug;
$catname = $cat->name;
break;
}
}
$counter++;

Resources