I have a front end form which will create two individual posts from the one form. My issue is when setting the custom taxonomy for each post, it will only input the first selection and not the full array of selections.
If I remove the [$key] from $services = $taxinput["job_listing_category"][$key];, it will then add all of the taxonomy selections from both inputs to both of the two posts.
I cant work out why I either have just a single taxonomy selection only applied to the correct individual post, or I have all taxonomy selections from both inputs added to both posts.
if($_POST){
foreach($_POST['class_name_1'] as $key =>$makepost)
{
if(strlen($makepost)>3){
$post_id = -1;
$taxinput = $_POST["tax_input"];
$services = $taxinput["job_listing_category"][$key];
$class_name_1 = $_POST['class_name_1'][$key];
$post_id = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_name' => '',
'post_title' => '',
'post_status' => 'pending',
'post_type' => 'job_listing',
'post_content' => ''
)
);
wp_set_post_terms( $post_id, $services, 'job_listing_category', true);
wp_add_post_meta( $post_id, 'class_name_1', $class_name_1);
}
}
}
Here is my frontend input form:
<form action="" method="post" id="submit-class" class="job-manager-form" enctype="multipart/form-data">
<div class="class-name-edit">
<label for="class_name">Class Name:</label>
<div class="field ">
<?php
get_job_manager_template( 'form-fields/text-field.php',
array(
'key' => 'class_name_1[]',
'field' => $all_fields['class_name_1']
)
);
?>
</div>
</div>
<div class="service-category classes">
<label for="job_category">Class categories:</label>
</div>
<?php
get_job_manager_template( 'form-fields/term-checklist-field.php',
array(
'key' => 'job_category[]',
'field' => $all_fields['job_category']
)
);
?>
</div>
</div>
<div class="class-name-edit">
<label for="class_name">Class Name:</label>
<div class="field ">
<?php
get_job_manager_template( 'form-fields/text-field.php',
array(
'key' => 'class_name_1[]',
'field' => $all_fields['class_name_1']
)
);
?>
</div>
</div>
<div class="service-category classes">
<label for="job_category">Class categories:</label>
</div>
<?php
get_job_manager_template( 'form-fields/term-checklist-field.php',
array(
'key' => 'job_category[]',
'field' => $all_fields['job_category']
)
);
?>
</div>
</div>
<div class="whitebox submitform">
<input type="submit" name="submit_job" class="button" value="Submit for Admin Approval" />
</div>
</form>
Related
Custom post type --- Event
custom taxonomy----- myevents
Using jQuery Datepicker for Start date and End date
Search field for searching event name
I want to fetch the custom post type events by using the simple form search. In which I fetched custom taxonomy in select option, then I added start and end date using jQuery Datepicker at last Search field.
My search URL looks like this:
http://localhost/customthemedev/?categoryfilter=Techfest&Start=01-May-2019&End=10-May-2019&search=smoke+event+
<form class="select-event-date" method="GET" action="<?php bloginfo('home'); ?>" autocomplete="off" id="filter">
<div class="form-group eventfunction">
<label>Categories</label>
<select class="form-control" name="categoryfilter">
<?php
$tax_terms = get_terms('myevents', array('hide_empty' => '0'));
foreach ( $tax_terms as $tax_term ):
echo '<option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';
endforeach;
?>
</select>
</div>
<div class="form-group selectDate">
<input type="text" class="form-control" placeholder="Start Date" name="Start" id="txtFrom">
</div>
<div class="form-group endDate">
<input type="text" class="form-control" placeholder="End Date" name="End" id="txtTo">
</div>
<div class="form-group searchEvent">
<label>
<img src="<?php echo get_template_directory_uri();; ?>/assets/images/search-22.png" alt="Icon">
</label>
<input type="text" placeholder="Search.." name="search">
</div>
<button type="submit" class="btn btn-default">FIND EVENTS</button>
</form>
Try this :
//Get all terms in array format
$termSlug = array();
$tax_terms = get_terms('myevents', array('hide_empty' => '0'));
foreach ( $tax_terms as $tax_term ):
$termSlug[] = $tax_term->slug;
endforeach;
//Assign the date into variable
$start_date = '01-May-2019';
$end_date ='10-May-2019';
//use the date format acccording to SQL format
$start_date_new = date("YOUR DATE FORMAT", strtotime($start_date));
$end_date_new = date("YOUR DATE FORMAT", strtotime($end_date));
$event_args = array(
'post_type' => 'Event', //Post type event
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1,
'meta_query'=>array( //Meta Query search
'relation'=>'AND',
array(
'key' => 'start_date',
'value' => $start_date_new, //start date here
'compare' => '>=',
'type' => 'NUMERIC'
),
array(
'key' => 'end_date',
'value' => $end_date_new, //end date here
'compare' => '<=',
'type' => 'NUMERIC'
)
),
'tax_query' => array(
array (
'taxonomy' => 'myevents', //Taxonomy search
'field' => 'slug',
'terms' => $termSlug, //terms in array
)
),
);
$all_events = new WP_Query($event_args);
echo '<pre>';print_r($all_events);echo '</pre>';
I have a form (a search form) on a single page. The code is below.
I wish to post the values of the form fields over to a pre_get_posts() function and then use the "properties" post type archive template (archive_properties.php) to then show the results.
I have posted my pre_get_post function below the form and I was hoping that would be sufficient.
Im getting the following error:
Warning: Invalid argument supplied for foreach() in /var/www/address/wp-admin/includes/plugin.php on line 1432
Ive got
function knoppys_search_form() { ?>
<form action="" id="propertySearch" name="propertySearch" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post" enctype="multipart/form-data" >
<div class="form-unit">
<select name="type">
<option value="">All</option>
<option value="Apartment">Apartments</option>
<option value="Villa">Villa</option>
</select>
</div>
<div class="form-unit">
<select name="location">
<?php $termsArgs = array( 'taxonomy' => 'locations' );
$terms = get_terms($termsArgs);
?>
<option value="">Location</option>
<?php foreach ($terms as $term) { ?>
<option value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
<?php } ?>
</select>
</div>
<div class="form-unit">
<input type="number" min="0" name="bedfrom" placeholder="Bed From">
</div>
<div class="form-unit">
<input type="number" min="0" name="bedto" placeholder="Bed To">
</div>
<input type="hidden" name="action" value="">
<button class="btn btn-primary" type="submit"><i class="fa fa-search"></i> Submit</button>
function knoppys_pre_get_filter() {
if (!is_admin() && is_archive('properties')) {
$location = $_POST['location'];
$type = $_POST['type'];
$bedfrom = $_POST['bedfrom'];
$bedto = $_POST['bedto'];
//Get the correct location
$tax_query = array(
'taxonomy' => 'locations',
'field' => 'id',
'terms' => $location
);
$query->set( 'tax_query', $tax_query );
//Get the correct type
$meta_query = array(
'relation' => 'AND',
array(
'meta_key' => 'type_name',
'value' => $type
),
array(
'meta_key' => 'number_of_beds',
'value' => array($bedfrom, $bedto),
'type' => 'numeric',
'compare' => 'BETWEEN',
)
);
$query->set( 'meta_query', $meta_query);
}
}
add_action('pre_get_posts', 'knoppys_pre_get_filter');
I build a form that enable users to post from the frontend.
I am trying to make it pass a taxonomy as well with the wp_set_object_terms becouse I use a custom post type and a custom taxonomy.
this is my code:
if(isset ($_POST['submit_offer'])=='submit'){
$user_id = get_current_user_id();
$args=array(
'post_author' => $user_id,
'post_title' => $_POST['job_title'],
'post_content' => $_POST['post_content'],
'post_excerpt' => $_POST['job_field'],
'meta_input' => array(
'points_amount' => $_POST['post_points'],
),
'post_status' => 'publish',
'post_type' => 'job_offers',
);
// get post id
$post_id = wp_insert_post($args);
$job_tax = array ( 44 , 45 );
wp_set_object_terms( $post_id , $job_tax, 'field_of_work' );
}
and Its not working!!!!!
Also for those who have custom post type & custom taxonomy and their code is in functions.php be sure to give a low priority (20) for the function that inserts the post(s). That will do the trick!
Here is an approach for wp_insert_post() in functions.php:
function insert_db_posts() {
$thepost = wp_insert_post(array(
'post_type' => 'art',
'post_title' => 'Try hard',
));
wp_set_object_terms( $thepost, 37, 'artist');
}
add_action( 'init', 'insert_db_posts', 20 ); // this will fire very late!!!
So, finally solved it. Just needed to wrap the form in a function and add it to init. :
<? php
function job_offer_form_init() {
// form to post test
function job_offer_form() {
$user_id = get_current_user_id();
$job_field_1 = userpro_profile_data('main_occupation', $user_id);
$job_field_2 = userpro_profile_data('sub_occupation_01', $user_id);
$job_field_3 = userpro_profile_data('sub_occupation_02', $user_id);
$form ='<div class="form-container">
<form action="" method="post" role="form">
<legend>הצעת שירותים/מוצרים</legend>
<div class="form-group">
<label for="job_title">כותרת ההצעה</label></br>
<input type="text" class="form-control" name="job_title" placeholder=""></br>
<label for="post_content">תוכן ההצעה</label></br>
<textarea class="form-control" name="post_content" placeholder=""></textarea></br>
<label for="job_field">תחום ההצעה</label></br>
<select name="job_field" multiple>
<option value="'.$job_field_1.'" selected>'.$job_field_1.'</option>
<option value="'.$job_field_2.'">'.$job_field_2.'</option>
<option value="'.$job_field_3.'">'.$job_field_3.'</option>
</select></br>
<label for="post_points">עלות בנקודות</label></br>
<input type="number" class="form-control" name="post_points" placeholder=""></br>
</div>
<button type="submit" name="submit_offer" value="submit" class="btn btn-primary">הגשת הצעה</button>
</form></div>' ;
return $form;
}
add_shortcode( 'jobofferform', 'job_offer_form' );
if(isset ($_POST['submit_offer'])=='submit'){
$user_id = get_current_user_id();
$args=array(
'post_author' => $user_id,
'post_title' => $_POST['job_title'],
'post_content' => $_POST['post_content'],
'post_excerpt' => $_POST['job_field'],
/* 'tax_input' => array(
'field_of_work' => array(44, 45),
), */
'meta_input' => array(
'points_amount' => $_POST['post_points'],
// 'job_field' => $_POST['job_field'],
),
'post_status' => 'publish',
'post_type' => 'job_offers',
);
// get post id
$post_id = wp_insert_post($args);
$job_tax = array ( 48 , 49 );
$term_taxonomy_ids=wp_set_object_terms( $post_id , $job_tax, 'field-of-work' , true );
if (is_wp_error($term_taxonomy_ids )) {
$error_string= $term_taxonomy_ids -> get_error_message();
add_post_meta( $post_id, 'taxonomies-error', $error_string );
} else {
add_post_meta( $post_id, 'taxonomies-error', 'no error' );
}
}
}
add_action( 'init', 'job_offer_form_init', 0 );
?>
Hi have a search form that is allows the user to input a keyword then select a taxonomy via dropdown list. This is my form:
<form name="myCity" id="myCity" action="http://mywebsite.com/" method="post">
<div class="search-area">
<div class="container">
<div class="row-fluid">
<div class="span4">
<label><i class="icon-search"></i></label>
<div class="search-area-division search-area-division-input">
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" placeholder="I am searching for..."/>
</div>
</div>
<div class="span3">
<label><i class="icon-map-marker"></i></label>
<?php
$args = array('taxonomy' => 'pa_city');
wp_dropdown_categories( $args );
?>
</div>
</div>
<noscript><input type="hidden" onclick="WriteCookie()" class="btn btn-block btn-white search-btn" value="<?php echo esc_attr__( 'Search' ); ?>"/></noscript>
<input type="hidden" name="post_type" value="product" />
</div>
</div>
If I search mini-golf in Bullhead City,
it returns the URL:
http://mywebsite.com/?s=mini-golf&cat=14&post_type=product
While it returns products with the tag mini-golf, it returns all products regardless of city that match mini-golf. I have tried many ways to filter the results and am just spinning my wheels at this point.
The taxonomy I am using is pa_city. Even if the url is
http://mywebsite.com/?s=mini-golf&pa_city=bullhead-city&post_type=product
it does the same thing.
No matter which way I write this, it will NOT exclude the other cities in the search. Should I try a NOTIN? I can't figure out why it brings all products regardless of city...
I needed to filter the query further in my functions.php using pre_get_posts
Whew, that was a frustrating one.
function my_location( $q ){
if (!$q->is_main_query() )
return;
if ($q->is_search()) {
$city = isset( $_COOKIE['city'] ) ? $_COOKIE['city'] : 'not set';
$q->set( 'tax_query', array(array( 'taxonomy' => 'pa_city', 'field' => 'slug', 'terms' => array( $city ), 'operator' => 'IN' )));
return;
}
if ($q->is_archive() ){
if ( ! is_admin() ) {
if (empty($_COOKIE['city'] )) {
echo "<script>window.location.href = 'http://mywebsite.com/select-city/';</script>";
exit();
}
$city = isset( $_COOKIE['city'] ) ? $_COOKIE['city'] : 'not set';
$q->set( 'tax_query', array(array( 'taxonomy' => 'pa_city', 'field' => 'slug', 'terms' => array( $city ), 'operator' => 'IN' )));
}}
}
add_action( 'pre_get_posts', 'my_location' );
I currently work for a site where I need to create posts from Front End.
This is my code to create a custom post from frontend of wordpress site.
<?php
$postTitleError = '';
if (isset($_POST['submitted']))
{
if(trim($_POST['postTitle']) === '')
{
$postTitleError = 'Please enter a title.';
$hasError = true;
}
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'product',
'post_status' => 'publish'
);
wp_insert_post( $post_information );
}
?>
And this is my HTML Form:
<form action="" id="primaryPostForm" method="POST">
<!-- Title -->
<input class="addTitle" type="text" name="postTitle" id="postTitle" class="required" />
<!-- Description -->
<input class="addDescription required" name="postContent" id="postContent" />
<!-- Submit button -->
<button type="submit"><?php _e('Add Product', 'framework') ?></button>
</form>
I shown all my tags on the page but I don't know how to assign them into the post.
Currently i am able to create posts using my code but all i need is to add tags into my post.
Any idea?
Have you tried adding the following to your $post_information = array():
'tags_input' => array('tag,tag1,tag2');
So your code should look like:
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'product',
'post_status' => 'publish',
'tags_input' => array('tag,tag1,tag2')
);
This assumes you have enabled tags in your custom post type.
Reference:
http://www.templatemonster.com/help/wordpress-how-to-enable-and-output-post-tags-for-custom-post-types.html
http://stackoverflow.com/questions/10434686/wordpress-wp-insert-post-not-inserting-tags
http://stackoverflow.com/questions/12267422/update-and-add-tags-post-on-save-post-using-wp-update-post