Front-End wordpress form not setting category - wordpress

I have a front-end wordpress form that submits to a custom post type and I'm calling the categories as so:
<select name="timesheets-category-dropdown">
<?php
$args = array(
'taxonomy' => 'timesheets_category',
'hide_empty' => false
);
$categories = get_terms( $args );
foreach( $categories as $category ) : ?>
<option value="<?php echo $category->name; ?>">
<?php echo $category->name; ?>
</option>
<?php endforeach; ?>
</select>
In my functions, I'm trying to set the category of the post as so:
$category_id = $_POST['timesheets-category-dropdown'];
$post_id = wp_insert_post($new_timesheet);
wp_set_object_terms( $post_id, $category_id, 'timesheets_category' );
The name of the category displays fine in something like this:
$title = $category_id . ' by: ' . $current_user->display_name . ' - ' . $week_ending;
but I can never get it to actually set the category of the post.
I've tried setting a hidden field with the ID of the category and pulling from that, but it's still the same result.

Related

Get Field values from another post type with ACF relationship

I'm using the following code to get a list of post titles which were set with ACF Relationship. Question is how to get custom field values on the other post type. (I only can get post meta info at the moment)
<form id="course-drop" name="course-drop" class="wpcf7-form" method="get" action="">
<select name="provider" id="provider" class="form-control">
<option value="">---</option>
<?php
$course_providers = get_posts(array(
'post_type' => 'course_providers',
'meta_query' => array(
array(
'key' => 'courses_offered',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
?>
<?php if( $course_providers ): ?>
<?php foreach( $course_providers as $course_provider ): ?>
<option value="<?php echo get_the_title( $course_provider->ID ); ?>" data-email="<?php echo the_field('email_address'); ?>"><?php echo get_the_title( $course_provider->ID ); ?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
check the data attribute I have tried which I need to get the email.
Ref used: https://www.advancedcustomfields.com/resources/querying-relationship-fields/
You can use the following to get the custom field of a post.
<?php echo the_field('email_address', 123); ?> //replace your post id with 123

Wordpress get_category_link with a custom template

I'm listing categories for a custom post type, which is going great so far.
The problem is when I get the category link, is linked to a default category archive template, and I'd like to link it to a custom-page-template.
Is there a way to do that?
<?php
//Llamar al catálogo
$taxonomy = 'lookbook-category';
$orderby = 'date';
$post_type = 'post';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'post_type' => $post_type,
);
?>
<?php
$categorias = get_categories($args);
// Mostrar categorías
foreach ($categorias as $cat) {
echo '<div class="temporadas-catalogo ">';
echo '<h1 class="">'.$cat->name.'</h1>';
echo '<p class="">'.$cat->description.'</p>';
echo '<div class="center margin-top">';
echo '<a class="button " href="'.get_category_link($cat).'">Ver Catálogo</a>';
echo '</div>';
echo '</div>';
}
?>
My assumptions are that you are using a custom Taxonomy called 'lookbook-category', with this you can set a custom taxononmy template like so:
taxonomy-{taxonomy}.php
The Wordpress developer docs is an excellent resource for understanding how the template hierarchy works.
https://developer.wordpress.org/themes/basics/template-hierarchy/

How do I display a "category products drop down in a wordpress page" in Woocommerce 2.5.2

I would like to display a drop down menu for products in a category.
<select>
<option value="CODE HERE">Volvo</option>
</select>
So according to Wordpress coding..
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Okay so I investigated further and I am looking to do a single page template according to https://developer.wordpress.org I am using a child theme for Storefront which is called NOVA WP.
To make this "single page template" I copied page.php and renamed it to page-buildit.php
This is Mypage in which I actually editing the code. I did copy the code but it turns out blank
found this: WooCommerce: Create a shortcode to display product categories
but my undestanding is we cant do this anymore with the new wordpress version.
<?php
$args = array(
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids,
'posts_per_page' =>'-1'
);
$product_categories = get_terms( 'product_cat', $args );
echo "<select>";
foreach( $product_categories as $category ){
echo "<option value = '" . esc_attr( $category->slug ) . "'>" . esc_html( $category->name ) . "</option>";
}
echo "</select>";
?>
Check this out. This is the way to get product categories.!
You can also use the function wp_dropdown_categories to make your code simpler.
To get a dropdown of categories of products you can write like this.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1);
wp_dropdown_categories($args);
Or if you want to hold the output in a variable you can use the argument 'echo'=>0 and then echo the variable to get the same output.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1,
'echo'=>0);
$cats = wp_dropdown_categories($args);
echo $cats;
Okay so here is how I solved it, with the help of Hemnath mouli, I already gave you the credit for the answer but I wanted to publish the products inside a category in a dropbox.
$args = array(
'posts_per_page' => -1,
'product_cat' => 'motherboard',
'post_type' => 'product',
'orderby' => 'title',
);
$products = new WP_Query( $args );
echo "<select>";
foreach ( $products as $product ) {
$products->the_post();
?>
<option value="<?php the_permalink(); ?>"> <?php the_title(); ?>
<?php
}
echo "</select>";
?>
Now I will need to show the image of this product after selecting it.

Get first two category only and excluding one in Wordpress

My code:
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
<?php
$category = get_the_category();
echo $category[1]->cat_name;
?>
I want get two category as links but exclude "uncategorized" category.
Try below code:
<?php
$args = array(
'include' => '1,2'
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '' . $category->name . '<br/>';
}
?>
In above code change 'include' => '1,2' and add the category that you want to include.
Find more here.
Updated :
If you want to get only first two category and exclude Uncategorized from that two category, then you can try below code :
<?php
$i=0;
$args = array(
'orderby' => 'id'
);
$my_category = get_categories($args);
foreach($my_category as $mcategory){
if($i==0 || $i==1){
if($mcategory->name != 'Uncategorized'):
echo '' . $mcategory->name . '<br/>';
endif;
}
$i++;
}
?>
Here I have added one dummy condition to make it work.

Getting the post ID outside outside of the Wordpress loop

So I have a snippet of code that grabs the categories and their coinciding posts and lists them outside of the loop (Below). I've been trying to get the post to link to #post-[ID] instead of the permalink - but I keep failing. Can anyone help?
<ul id="sidebar">
<?php
foreach( get_categories('orderby=ID&order=desc') as $cat ) :
if( !$cat->parent ) {
echo '<li class="title"><h2>' . $cat->name . '</h2>';
echo '<ul>';
process_cat_tree( $cat->term_id );
}
endforeach;
wp_reset_query(); //to reset all trouble done to the original query
//
function process_cat_tree( $cat) {
$args = array('category__in' => array( $cat ), 'numberposts' => -1);
$cat_posts = get_posts( $args );
$id = $post->ID;
global $post;
if( $cat_posts ) :
foreach( $cat_posts as $menuPost ) :
echo '<li';
if ( $menuPost->ID == $post->ID ) { echo ' class="active"'; }
echo '>';
echo '' . $menuPost->post_title . '';
echo '</li>';
endforeach;
endif;
echo '</ul></li>';
}
?>
The above code is outputting UL/LI tags like this:
CATEGORY
Post
Post
Post
CATEGORY
Post
Post
Post
CATEGORY
Post
Post
Post
Admittedly, I don't exactly understand what you mean by "linking to #post-[ID]", but going with the question title:
If you use get_permalink() when echoing the link, you will get the permalink - that's just what that function does.
Use get_the_ID() instead, if you want the post-ID returned, or the_ID() if you want it displayed (the_ID() is the same as echo get_the_ID()).
Edited from here:
If you're otherwise happy with the above code, changing
echo '' . $menuPost->post_title . '';
to
echo '' . $menuPost->post_title . '';
ought to do it.
However, I'd go about it like so:
echo '<ul>';
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<li class="title"><h2>' . $category->name . '</h2><ul>';
// query posts of that category:
$query_args = array(
'cat' => $category->cat_ID,
'posts_per_page' => -1
);
$your_query = new WP_Query( $query_args );
// loop through them:
while ( $your_query->have_posts() ) : $your_query->the_post();
echo '<li><a href="#post-';
the_ID();
echo '">';
the_title();
echo '</a></li>';
endwhile;
echo '</ul></li>';
// Reset Post Data
wp_reset_postdata();
}
echo '</ul>';

Resources