Edit form for custom loop post - php

So, I have a custom loop post.
On the page, I have 10 posts as below:
<?php
$args = array(
'post_type' => 'mail',
'paged'=>$paged,
'posts_per_page' => 10,
'mail_cat' => '',
'orderby' => 'date',
'order' => 'DESC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
Each post gets title, content and price based on the data-mail_id as below:
<div data-mail_id="' . esc_attr( $mail->id ) . '">
<?php the_title(); ?>
<?php the_content(); ?>
<?php echo $mail->get_price_html(); ?>
</div>
Now, how can I add an edit form (<form>) to edit the post contents?
I am guessing I need following:
Get the <form> with the targeted post-mail_id data such as title.
Change the contents of the post
Submit the change which will update the data for that specific post-mail_id.
What do you guys think?
EDIT 1:
Well, I am not sure if this is correct, but it is a start :p
<form role="form" method="post">
<?php wp_nonce_field( 'update_post_'. get_the_ID(), 'update_post_nonce' ); ?>
<div id="title_change">
<input type="text" id="post_title" name="post_title" value="<?php echo $post->post_title; ?>"></input>
<input type="submit" name="update" type="submit" ></input>
</div>
</form>
So, now what? lol
So, I can show the original title in the input field.
I am not sure how the change can be submitted and be updated.

Related

Wordpress - Custom Post Type & Custom Field Issues

I seem to be getting nowhere with this.
I have custom post types set up as Areas.
I'm trying to call all pages under the CPT "areas" into a select box.
<form action="<? bloginfo('url'); ?>" method="get">
<select name="page_id" id="page_id">
<?php
global $post;
$args = array(
'numberposts' => -1,
'show_option_none' => __( 'None' ),
'orderby' => 'title',
'hide_empty' => false,
'post_type'=>'areas',
'hierarchical' => true,
'suppress_filters' => true);
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<? echo $post->ID; ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="view" />
</form>
This code works but cancels out other custom fields being called further on down the page.
For example, this bit of code works if the other code is removed:
<img src="<?php
if(get_post_meta($post->ID, 'mainimg', true)) {
echo get_post_meta($post->ID, 'mainimg', true);
}
?>">
I'd really appreciate any help. I have no idea what's causing this issue. The closest I came was thinking it's to do with the post ID part but I am genuinely at a loss.
Thank you in advance.
SOLVED
Thanks to Huseyin
Changed $post to $post2
Also changed <?php the_title(); ?> to <?php echo $post2->post_title; ?> as Wordpress was showing the current page title for all options.

Submit value from first dropdown to second dropdown without reloading the page

I have a repair prices calculator on my website, where users can first select the brand from a dropdown and then select the exact model from the second dropdown.
Currently the page reloads after selecting an option from the first dropdown and passes the selected value with $_POST, the second dropdown then shows all devices from the selected brand. After selecting the model, the user gets forwarded to the specific device page.
I would prefer to pass the value and show the second dropdown without the page being reloaded. I have tried doing this with ajax, but wasn't able to get the correct code, so I went back to the first working setup. Could anyone tell me the code I need to submit the value to the second dropdown without reloading?
This is the code I have so far:
<h2>Preisrechner</h2>
<div class="preisrechner-select-wrapper">
<form method="POST" action="">
<div>
<?php
$args = array(
'hierarchical' => 1,
'depth' => 1,
'orderby' => 'name',
'echo' => 0,
'taxonomy' => 'marke',
// this leads to variable name $_POST['marke']
'name' => 'marke-sel'
);
if( ! isset($_POST['marke-sel']) ):
$args['show_option_none'] = 'Hersteller auswählen';
else:
$args['selected'] = $_POST['marke-sel'];
endif;
$marke = wp_dropdown_categories( $args );
// this enables the buttonless js possibility
$marke = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $marke);
echo $marke;
?>
<noscript>
<div>
<input type="submit" value="marke"/>
</div>
</noscript>
</div>
</form>
<?php
if( isset($_POST['marke-sel']) && $_POST['marke-sel'] ):
?>
<form method="POST" action="<? bloginfo('url'); ?>">
<input type="hidden" name="marke" value="<?php echo $_POST['marke-sel'] ?>">
<div>
<?php
$the_query = new WP_Query( array(
'post_type' => 'reparaturpreise',
'tax_query' => array(
array (
'taxonomy' => 'marke',
'field' => 'id',
'terms' => $_POST['marke-sel'],
)
),
) );
if ( $the_query->have_posts() ) :
?>
<div class="form-option parent-field-wrapper">
<label for=""/>
<select name='modell' id='modell' onchange='document.location=this.value'>
<option value="">Modell auswählen</option>
<?php while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<option value="<?php the_permalink();?>">
<?php the_title(); ?>
</option>
<?php endwhile; ?>
</select>
</div>
<?php endif;
wp_reset_postdata();
$modell = preg_replace("#<select([^>]*)>#", "<select$1 onchange='this.form.submit()'>", $modell);
echo $modell;
?>
<noscript>
<div>
<input type="submit" value="modell"/>
</div>
</noscript>
</div>
</form>
<?php endif; ?>
<?php
if( isset($_POST['marke-sel']) && $_POST['modell'] ):
$args = array(
'post_type' => 'reparaturpreise',
'cat' => $_POST['marke-sel'],
'posts_per_page' => 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
endif;
?>
</div>
If the code generated by PHP needs to change (ie results) then the page will need to reload. The only way around this would be to move this code to an endpoint which can be called via an AJAX call and returns the results.
A good example which can be changed for your scenario can be found here:
https://premium.wpmudev.org/blog/using-ajax-with-wordpress/

Modifying category_name arg with a checkbox form Wordpress

I want to build an explore page template on my Wordpress website where a user can visit the page and by default, posts in all categories will be shown (This I can do). I would like to add a form with checkboxes at the top of the page where the user can narrow down their search. The user should be able to select multiple categories and once they click submit the query will update to show only posts in those categories. Below, I will post the two pieces of code I have at this moment.
This code creates the form that displays the inputs and holds the category_name value. Right now, it sends the user to the category page upon submit and that's not what I need it to do.
$zones = get_field('zones_list');
$business_types = get_field('business_type');
echo'<form role="search" method="get" id="searchform" action="'.home_url('/').'">
<div>';
foreach( $zones as $zone ):
echo'<input type="checkbox" value="'.$zone->slug.'" name="category_name">'.$zone->name.'</input>';
endforeach;
echo'</div>
<div>';
foreach( $business_types as $business_type ):
echo'<input type="checkbox" value="'.$business_type->slug.'" name="category_name">'.$business_type->name.'</input>';
endforeach;
echo'</div>
<input type="submit" id="searchsubmit" value="Submit" />
</form>';
This code holds the args for the loop i'm creating on the page.
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'category_name' => '',
'order' => 'DESC',
'posts_per_page'=> '3',
'paged' => get_query_var( 'paged' ),
);
global $wp_query;
$wp_query = new WP_Query( $args );
if( $wp_query->have_posts() ):
while( $wp_query->have_posts() ): $wp_query->the_post(); global $post;
I now that if I manually add multiple category names in the category_name field like this
'category_name' => 'zone1,zone2',
The query displays only posts in those categories. Which is what I need it to do, but I need the user to be able to select the categories they would like to see via a form on the front-end.
Is there a way to create a form on the front-end of my website that can update the "category_name" section when a user selects the checkboxes and clicks submit?
UPDATE
I found a solution. I'm not sure it's the correct way to do it, but it works.
This is the updated code
$zones = get_field('zones_list');
$business_types = get_field('business_type');
echo'<form role="search" method="post" id="searchform">
<div>';
foreach( $zones as $zone ):
echo'<input type="checkbox" value="'.$zone->slug.'" name="category_name[]">'.$zone->name.'</input>';
endforeach;
echo'</div>
<div>';
foreach( $business_types as $business_type ):
echo'<input type="checkbox" value="'.$business_type->slug.'" name="category_name[]">'.$business_type->name.'</input>';
endforeach;
echo'</div>
<input type="submit" id="searchsubmit" value="Submit" />
</form>';
if(isset($_POST['category_name'])) {
$cat_name = $_POST['category_name'];
$cat_formated = implode('+', $cat_name);
}
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'category_name' => $cat_formated ,
'order' => 'DESC',
'posts_per_page'=> '3',
'paged' => get_query_var( 'paged' ),
);
global $wp_query;
$wp_query = new WP_Query( $args );
if( $wp_query->have_posts() ):
while( $wp_query->have_posts() ): $wp_query->the_post(); global $post;

Why I'm getting only one post with get_posts in WordPress?

UPDATE: I now know why didn't worked. It was because I was trying to show a private post. Dumm me. Happens to the best of us. Sorry. And thanks for all your help.
I'm trying to list all posts with certain meta key and meta value but I can't get all the posts with that certain meta value. I'm only getting one.
I have a foreach loop and the next code to list all my posts like this:
$args = array(
'post_type' => 'produs',
'meta_key' => 'sticky_post',
'meta_value' => 1
);
$posts = get_posts($args);
<?php
foreach($posts as $post){ ?>
<div class="product" id="product-<?php echo $post->ID; ?>">
<div class="thumb new_product_thumb">
<?php
$thumb_args = array('class' => 'product-img', 'alt' => the_title_attribute( 'echo=0' ) );
?>
<a class="product-thumb read-more" style="" href="<?php echo the_permalink(); ?>" title="citește mai departe">
<?php echo the_post_thumbnail('product-listing', $thumb_args); ?>
</a>
<?php //if ( current_user_can('manage_options') ) {
if (get_field('tva_produs',$post->ID) == '9') echo '<div class="tva_redus"></div>';
// var_dump($top_sellers_new);
//}
?>
</div>
<h2>
<a class="read-more" style="" href="<?php echo the_permalink(); ?>" title="citește mai departe">
<?php the_title(); ?>
</a>
</h2>
<div class="product-content">
Pret: <?php echo get_field('pret_nou', $post->ID); ?> lei
</div>
<a class="read-more more2" href="<?php echo the_permalink(); ?>" title="citește mai departe"><p>Detalii produs</p> <span class="arrow-next"> </span></a>
<?php $id = get_the_ID();?>
<form id="adauga_in_cos" action="<?php echo THEME_URL; ?>/product.php?action=add&product=<?php echo $id; ?>" method="post">
<input type="hidden" name="produs_id" id="produs_id" value="<?php echo $id; ?>" />
<input type="hidden" name="produs_price" id="produs_price" value="<?php echo get_field( 'pret_nou', $id ) ?>" />
<input type="hidden" name="produs_name" id="produs_name" value="<?php echo $post->post_title; ?>" />
<!-- <input id="adauga_but" class="add_to_sc" type="submit" value="Adauga in cos" />-->
</form>
<div class="horizontal"></div>
</div>
<?php } ?>
Why do I get only one post with meta_value 1, instead of all with meta_value 1 ?
Three issues here
Do not abuse the the $posts global, you are using it as a custom variable, so you are breaking the global. Use a custom variable name, something like `$posts_array.
If you are going to use $post, (which you should use to setup postadat), you should reset it back to its original value
Set posts_per_page to -1 to get all posts with the desired custom field
Your code should look something like this: (Untested)
$args = array(
'post_type' => 'produs',
'meta_key' => 'sticky_post',
'meta_value' => 1,
'posts_per_page' => -1
);
$posts_array = get_posts($args);
foreach ( $posts_array as $post ) {
setup_postdata( $post );
// All your other code
}
wp_reset_postdata();
If this does not work, you should make sure of that you do not have a bad instance of the pre_get_posts action
have you tried adding
'numberposts' => -1,
This should make get_post get all the posts available, if that doesn't work we can't help you further unless you provide more code
Try this, You can use posts_per_page argument, and set it -1
$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', 'post_parent' => $post->ID );
$attachments = get_posts( $args );
Please try to use WP_Query rather than get_posts
If you only need an array of posts, and don't need the query object -use get_posts(). Otherwise, if you do need the access to the query object methods, or pagination, or sticky posts at the top, you should use WP_Query.
Normally (by default with WP_Query object) - WordPress queries how many posts there are in total - even if you are only after the first 10. It does this so it can perform pagination. So get_posts() is actually (slightly) quicker (it also ignores sticky posts).
'meta_value' - Note that a 'meta_key=keyname' must also be present in the query. Note also that the sorting will be alphabetical which is fine for strings (i.e. words), but can be unexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as you might naturally expect).
$args = array(
'post_type' => 'produs',
'meta_key' => 'sticky_post',
'meta_value' => 1,
'posts_per_page' => -1
);
$myposts = get_posts($args);
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?>
<?php endforeach; wp_reset_postdata(); ?>

Adding a data attribute to a html content (html content getting added in product looping from Custom Fields)

I am using woocommerce .I am looping into products to list them.
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 100, 'product_cat' => 'sections', 'order' => 'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' );
the_field('html_content');
?>
<input type="hidden" name="price_section" value="<?php echo$saleprice = get_post_meta( get_the_ID(), '_sale_price', true); ?>" />
<img src="<?php echo $image[0]; ?>" />
<p><?php echo the_title(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
In above code the_field('html_content'); is the content i am getting from custom field and its value is like this.
<div class="dvThumb " data-section="One">
<div id="asd" name="" class="dvSectionCol" style="width:90.2px
!important; text-align:center;" >
</div>
Now in looping I want to adda data attribute to the html_content (div with id as asd). please tell me how is it possible to add a data attribute to each html_content recieved in loop.
What I want in that data attribute is price .What I have tried is what you can see in loop that I made a hidden input type but I dont see any way to have a unique id and name for each price. So is there any way I can add a data attribute to each html_content in loop itself.
It would be great if I can add a data attribute for price because than it will be very easy for me for further use.
Ok I got the solution ..I added one more custom field which stores the ID of the div (asd in my case) and in loop itself i use that custom field storing ID to assign a data attribute to my html_content ;)
<script>
var for_id='<?php echo$for_id; ?>';
$("#"+for_id).attr('data-price',<?php echo$saleprice =
get_post_meta( get_the_ID(), '_sale_price', true); ?>);
</script>

Categories