I put together a couple of dropdown searches and I'm having problems with the authors.
This is the website for my archives page: http://goo.gl/p1RLUm
So if I search for an author it sends me to something like this:
mydomain.com/?author=67
When it should be sending me to this:
mydomain.com/author/username
here's an actual working author page
I've tried so many things but it always pull the query string as an answer and can never get it to go away.
Any help is appreciated!
<form action="<?php bloginfo('url'); ?>/author/" method="get">
<?php
$args = array(
'show_option_none' => __( 'Select Author' ),
'name' => 'author',
'orderby' => 'ASC',
'echo' => 0,
'who' => 'authors',
);
?>
<?php $select = wp_dropdown_users( $args ); ?>
<?php $replace = "<select$1 onchange='return this.form.submit()'>"; ?>
<?php $select = preg_replace( '#<select([^>]*)>#', $replace, $select ); ?>
<?php echo $select; ?>
<noscript>
<input type="submit" value="View" />
</noscript>
</form>
Ok so updated my code to this. Am I on the right track?
<?php $users = wp_list_authors($args); ?>
<?php
$args = array(
'show_option_none' => __( 'Select Author' ),
'name' => 'author',
'orderby' => 'ASC',
'echo' => FALSE,
'who' => 'authors',
);
?>
<select name="author-dropdown" onchange='return this.form.submit()'>
<?php foreach($users as $user):?>
<option name="<?php echo $user->name; ?>">
<?php echo $user->name; ?>
</option>
<?php endforeach;?>
</select>
Unfortunatly for you the wp_dropdown_users( $args ) function does not have an option to set the value of names. It is the ids and that's it.
What you should do is use wp_list_authors( $args ) and then do something like this :
<?php $users = wp_list_authors($args); ?>
<select name="yourname" onchange='return this.form.submit()'>
<?php foreach($users as $user):?>
<?php //just check how to access the $user name url friendly tag ?>
<option name="<?php echo $user->name; ?>"><?php echo $user->name?></option>
<?php endforeach;?>
</select>
By doing so you don't need your replace/regex thing and have more control over the users output. I'm guessing your JS does the window.location.href = redirection job.
Plus, there's an error with your redirection. You should remove the ? character in your url at submit. I guess it is also done in your JS.
Related
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.
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/
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.
I have a page that displays all the staff members, which are grouped by company. Each staff member is a custom post type. I have a dropdown menu and a submit button to filter the staff by company, which works fine.
<div id="choose">
<form name="selectCompany" method="post" action="">
<span>Select Company: </span>
<select name="company" >
<option value="All Staff">All Staff</option>
<option value="Company 1" <?php echo $yourgroup; ?>>Company 1</option>
<option value="Company 2" <?php echo $yourpower; ?>>Company 2</option>
<option value="Company 3" <?php echo $yourelectrical; ?>>Company 3/option>
<option value="Company 4" <?php echo $newmills; ?>>Company 4"</option>
</select>
<input type="submit" value="Go" name="submit" />
</form>
</div>
The Wordpress query I'm using is (where $company is the value from the dropdown menu):
if ($company != "All Staff") {
$mypost = array( 'post_type' => 'staff_members',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'company',
'value' => $company,
'compare' => '=',
),
),
);
}
else {
$mypost = array( 'post_type' => 'staff_members',
'orderby' => 'title',
'order' => 'ASC',);
}
if (empty($_POST['submit'])) { //Set default value for displaying staff
$mypost = array( 'post_type' => 'staff_members',
'orderby' => 'title',
'order' => 'ASC',);
$company = "All Staff";
}
?>
<p><span class="display">Displaying Staff for <?php echo $company; ?></span></p>
<?php
$loop = new WP_Query( $mypost );
while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="staff">
<div class="photo">
<a href="<?php the_permalink(); ?>" rel="shadowbox[staff-1]">
<?php
if ( '' != get_the_post_thumbnail() ) { //Recommended way of doing it
the_post_thumbnail();
}
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/staff-default.gif" />';
}
?>
</a>
</div>
<div class="name"> <?php the_title(); ?></div>
<div class="title">
<?php echo esc_html( get_post_meta( get_the_ID(), 'job_title', true ) ); ?>
</div>
<?php // echo esc_html( get_post_meta( get_the_ID(), 'company', true ) ); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
This all works fine, however I want to remove the submit button and just do it when the list changes. I changed it to <select name="company" onchange="this.form.submit()">. It submits the form and the page reloads but it doesn't actually filter the staff. I don't know why using a submit button works but but using onchange doesn't.
i make meta box and in this i make select option with multiple
$opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);
<select name="opt_meta_author" id="opt_meta_author" multiple="multiple">
<?php
$auth_args = array(
'post_type' => 'author',
'orderby' => 'title',
'order' => 'ASC'
);
$authors = new WP_Query($auth_args);
while($authors->have_posts()) :
$authors->the_post();
?>
<option value="<?php echo the_title() ?>">
<?php echo the_title() ?>
</option>
<?php
endwhile;
?>
</select>
when i select multiple options it save only one option, i want to save selected options
is there any suggestions
Saving meta values
$opt_meta_author = $_POST['opt_meta_author'];
update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author);
$opt_meta_author = unserialize(get_post_meta($post->ID, 'opt_meta_author', true));
<select name="opt_meta_author" id="opt_meta_author" multiple="multiple">
<?php
$auth_args = array(
'post_type' => 'author',
'orderby' => 'title',
'order' => 'ASC'
);
$authors = new WP_Query($auth_args);
while($authors->have_posts()) :
$authors->the_post();
?>
<option value="<?php echo the_title() ?>">
<?php echo the_title() ?>
</option>
<?php
endwhile;
?>
</select>
To get multiple selected value while storing do this:
$opt_meta_author = serialize($_POST['opt_meta_author']);
What is the var dump of $_POST['opt_meta_author'])? if it is an array, convert it in string using implode and save the string in database