I'm building a meta box for one of my custom post types that has a selection option. I'm trying to make it so that when you make a selection and update the post, the option stays selected when the page reloads. I've found a few people on StackOverflow working on the same and I've followed those suggestions but haven't quite figured it out yet. If anyone has any suggestions, any help is appreciated.
<?php
function property_info_meta_box() {
add_meta_box('property-info', 'Property', 'property_info_cb', 'properties', 'normal', 'high');
}
add_action('add_meta_boxes', 'property_info_meta_box');
function property_info_cb($propertyInfo) {
$selectAgent = get_post_meta($propertyInfo->ID, 'select-agent-value', true);
?>
<label for="select-agent-text">Select Agent</label> <br>
<select multiple size="5" name="select-agent" id="select-agent">
<option value="none">None</option>
<?php
$args = array(
'post_type' => 'agents',
'posts_per_page' => -1
);
$posts = new WP_Query($args);
if ( $posts->have_posts() ) : while ( $posts->have_posts() ) : $posts->the_post(); ?>
<option value="<?php the_ID() ?>" <?php selected( $selectAgent, the_ID() ); ?>> <?php the_title(); ?> </option>
<?php endwhile; endif; ?>
</select>
<?php }
function add_property_info_fields ($propertyInfoId, $propertyInfo){
if ($propertyInfo->post_type == 'properties') {
if(isset($_POST['select-agent'])){
update_post_meta($propertyInfoId, 'select-agent-value', $_POST['select-agent']);
}
}
}
add_action('save_post', 'add_property_info_fields', 10, 2);
Shot in the dark here, but you are using the_ID() improperly. This function prints the ID to the screen. You are trying to return the ID for use as a function parameter. You should try something like:
<?php selected( $selectedAgent, get_the_ID() ); ?>
See get_the_ID() vs the_ID()
Related
I have been trying to display wordpress blog post categories in a html/php website.
This is my code.
<select class="form-control" name="Category">
<option>Select Option</option>
<?php
include('blog/wp-load.php');
$category_list = wp_get_post_categories();
foreach($category_list as $category){ ?>
<option><?php echo $category->category_name; ?></option>
<?php } ?>
</select>
Its not working. What is wrong with my code? Is this the right method?
Can anyone please help me with the solution?
wp_get_post_categories() require post_id to render data of post if you don't pass post id wordpress get post_id from global $post object. In your case you are including wordpress outside of it's scope so this function never work.
Use get_terms() to get term of your taxonomy.
include('blog/wp-load.php');
$category_list = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
?>
if( !empty( $category_list ) && !is_wp_error( $category_list ) ){
?>
<select class="form-control" name="Category">
<option>Select Option</option>
<?php foreach( $category_list as $category ){ ?>
<option value="<?php echo $category->term_id; ?>"><?php echo $category->name; ?></option>
<?php } ?>
</select>
<?php } ?>
Although I do not recommend this method instead of this you can call WordPress API endpoint to get your taxonomy. You can refer here
I am looking for a way to retain the selection from a a list of options which are created from within a WordPress post loop:
<?php
$args = array( 'post_type' => 'office_locations', 'posts_per_page' => -1, 'order_by' => 'title', 'order' => 'ASC' );
$loop = new WP_Query( $args ); ?>
<select style="width: 100%;" name="selectedValue" onchange="this.form.submit()">
<option disabled>Select an office location...</option> // This is disabled
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<option><?php echo get_the_title();?></option>
<?php endwhile; ?>
</select>
So if a User makes a selection I have set it to post that upon selection, is there a way for the select to remain on that option after this happens?
Sure, check the value of POST matches the value of the select, then set as selected:
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<option<?= (isset($_POST['selectedValue']) && $_POST['selectedValue'] == get_the_title() ? ' selected' : null) ?>><?php echo get_the_title();?></option>
<?php endwhile; ?>
I'm using a custom wp-query on my website to display all pages from a parent page inside a list style with select, when selecting a page I'm redirected to the right page.
works fine :
here is my code :
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => '22',
'order' => 'ASC',
'orderby'=>'meta_value',
'meta_key'=>'nom_de_loeuvre'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<span class="styled-select">
<select name="" onchange="location = this.options[this.selectedIndex].value;">
<option>A - Z</option>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_field('nom_de_loeuvre'); ?>
, de
<?php if(get_field('prenom_artiste')): ?>
<?php the_field('prenom_artiste'); ?>
<?php endif ;?>
<?php the_field('nom_artiste'); ?> | Galerie
<?php if(get_field('prenom_galerie')): ?>
<?php the_field('prenom_galerie'); ?>
<?php endif ;?>
<?php the_field('nom_galerie'); ?>
</option>
<?php endwhile; ?>
</select>
</span>
<?php endif; wp_reset_query(); ?>
no I'm trying to hightlight the active page of my list, using "option selected value".
when on a page, I want the select list to display the current page in the liste.
Can anybody help me with this ?
I can't find a solution or a way to do it on the web...
I'm not sure if it's possible, but I guess if it is, It must be something to add in my Wp-query or maybe in jquery ?
thanks a lot for your help...
I'm using a php code to display children pages from a specific parent page ID to display page links in a select list type.
all my children pages have a sub_field called "artiste", with an artist name.
here is my php code to display the page name with link to the page :
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => '22',
'order' => 'ASC',
'orderby' => 'title'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<div class="styled-select">
<select name="" onchange="location = this.options[this.selectedIndex].value;">
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></option>
<?php endwhile; ?>
</select>
</div>
<?php endif; wp_reset_query(); ?>
this works fine, I get all the children page titles and the permalink and sorted alphabetically.
now I want to get instead of the page title, a custom field from each children pages, here is my code :
my custom field is "the_sub_field('nom_artiste');"
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => '22',
'order' => 'ASC',
'orderby' => 'title'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<div class="styled-select">
<select name="" onchange="location = this.options[this.selectedIndex].value;">
<option>--- A - Z ---</option>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php if(get_field('artiste')): while (has_sub_field('artiste') ): ?><?php the_sub_field('nom_artiste'); ?><?php endwhile; endif; ?></option>
<?php endwhile; ?>
</select>
</div>
<?php endif; wp_reset_query(); ?>
it works also, I get the custom field instead of the page title, and the permalink of my page title, that's perfect.
but in this second code, my custom field are sorted depending of there page title.
for example, I have 2 pages :
AAAAA
ZZZZZ
and custom fields :
for AAAA : custom field is GGGG
for ZZZZ : custom field is BBBB
so with the second php code, my list is not sorted right because it sorted depending on the page title and not of custom field... I get :
GGGG
BBBB
I think I have to create an array with my custom field to be abble to sort them alphabetically before displaying them in my select onchange, but I don't know how to do it...
can anybody help me with this ?
thanks a lot for your help
Try this, replacing everything between <?php while ... endwhile; ?> in your code:
<?php
//Declare an array to hold data
$options = array();
//The loop
while ( $parent->have_posts() ) : $parent->the_post();
//Define a temporary array
$option = array();
//Get all the values we need and store them in an array.
$option['value'] = get_permalink();
$option['title'] = get_the_title();
if(get_field('artiste')):
while (has_sub_field('artiste') ) {
$option['label'] = get_sub_field('nom_artiste');
}
else:
$option['label'] = get_the_title(); //Put something here if artiste is not populated.
endif;
$options[] = $option;
endwhile;
//Sort the array on the 'label' field
usort($options, function($a, $b) { return strcmp($a['label'], $b['label']); });
//Output the <option> tags
$format = ' <option value="%s" title="%s">%s</option>'."\n";
foreach ($options as $o) {
printf($format, $o['value'], $o['title'], $o['label']);
}
?>
I'm following a guide at the moment and have managed to create a custom post type and meta boxes. However, getting the meta data to display in a post just isn't happening for me at the moment.
All i want is the meta box to have 3 custom text fields that I can then output in a post.
This is my functions file (the part that defines the post type and meta boxes):
add_action('init', 'product_manager_register'); // Calls function to set up post-type
function product_manager_register() {
$args = array(
'label' => __('Product Manager'),
'singular_label' => __('Skin'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'rewrite' => array('slug' => 'skins', 'with_front' => false),
);
register_post_type('products' , $args ); // runs the function
}
//Add featured image support to the theme
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
}
add_action("admin_init", "product_manager_add_meta");
function product_manager_add_meta(){
add_meta_box("product-meta", "Product Options", "product_manager_meta_options", "products", "normal", "high");
}
function product_manager_meta_options(){
global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
$custom = get_post_custom($post->ID);
$buylink = $custom['buylink'][0];
$price = $custom['price'][0];
$previewlink = $custom['previewlink'][0];
?>
<style type="text/css">
<?php include('productmeta.css'); ?>
</style>
<div class="product_extras">
<div> <label> Buy Link: </label> <input name="buylink" value="<?php echo $buylink; ?>" /></div>
<div> <label> Price: </label> <input name="price" value="<?php echo $price; ?>" /></div>
<div> <label> Preview Link: <input name="previewlink" value="<?php echo $previewlink; ?>" /></div>
</div>
<?php
}
add_action('save_post', 'save_product_meta');
function save_product_meta(){
global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
return $post_id;
}else{
update_post_meta($post->ID, "buylink", $_POST["buylink"]);
update_post_meta($post->ID, "price", $_POST["price"]);
update_post_meta($post->ID, "previewlink", $_POST["previewlink"]);
}
}
?>
And I display the information in single-products like this:
<?php get_header() ?>
<div class="container content"><!-- Begin maincontent Container -->
this is the page i'm editing
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$custom = get_post_custom($post->ID);
$buynowlink = $custom ["buynowlink"][0];
$price = $custom ["price"][0];
$previewlink = $custom ["previewlink"][0];
?>
<div class="post group">
<h2><?php the_title(); ?> </h2>
<?php the_content(); ?>
<?php print "<p>$buynowlink</p>"; ?>
<?php print "<p>$price/p>"; ?>
<?php print "<p>$buynowlink</p>"; ?>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
I know I'm probably doing something stupid but any help would be really appreciated. I know I could do this with a plugin but I'd rather learn to do it the proper way.
You just have the wrong ID's in place
so to output correctly its should be:
<?php
$custom = get_post_custom($post->ID);
$buynowlink = $custom ["buylink"][0];
$price = $custom ["price"][0];
$previewlink = $custom ["previewlink"][0];
?>
and to print it - you have the buy link in the previews rule as well as an open p tag so it should be:
<?php print "<p>$buynowlink</p>"; ?>
<?php print "<p>$price</p>"; ?>
<?php print "<p>$previewlink</p>"; ?>
Hope this helps
I think I see a couple of syntax errors in your code, such as the way you want to print the custom field's values and so on.
I would recommend this approach as opposed to the approach you are using, and I'll explain why in a bit, after the code.
PHP WordPress Loop with get_post_meta();
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!--This is a conditional statement to see if the value of the custom field has something, and if it does then it shows it, otherwise it doesn't render anything-->
<?php if ( get_post_meta($post->ID, 'cf-size', true)):?>
<h1>Custom Field Value: <?php echo get_post_meta($post->ID, 'cf-size', true);?></h1>
<?php endif;?>
<?php endwhile; endif; ?>
You can notice that cf-size is the name of the custom field and that I'm checking for it's value on the current post. The code above will surely work as I've used it many times in my own creations.
Here is an example of how to pull 3 fields with the same if statement... just remember that if the condition doesn't validate as "true" (meaning that all 3 fields have a value) then none will show even if 2 or 1 does have a value.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!--This is a conditional statement to see if the value of the custom field has something, and if it does then it shows it, otherwise it doesn't render anything-->
<?php if ( get_post_meta($post->ID, 'cf-size', 'cf-color', 'cf-brand', true)):?>
<h1>Custom Field Value for Size: <?php echo get_post_meta($post->ID, 'cf-size', true);?></h1>
<h1>Custom Field Value for Color: <?php echo get_post_meta($post->ID, 'cf-color', true);?></h1>
<h1>Custom Field Value for Brand: <?php echo get_post_meta($post->ID, 'cf-brand', true);?></h1>
<?php endif;?>
<?php endwhile; endif; ?>