• Hi there,
    This is driving me mad!
    I am trying to pass the value choosen from a dropdown into a second set of code. I have two ways of creating the dropdown , both of which work (I prefer the look of the first).
    First way:

        <div class="gallery-row">
    <li id="categories">
    	<h2><?php _e( 'Categories:' ); ?></h2>
    	<form id="category-select" class="category-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
    		
    		<?php $args = array(
        'taxonomy'    => 'download_category', // EDD custom post type Taxonomy
        'order'       => 'ASC', // Order of the list
    		'value_field' => 'slug', // <strong>Should be name?</strong>
    			);
    		wp_dropdown_categories($args); ?>
    		
    		<input type="submit" name="submit" value="view" />
    	</form>
    </li></div>

    Second way:

    <?php //--This is the drop down START--
    
     echo '<select name="categories">';
      // Add custom option as default
      echo '<option>' . __('No Category', 'text-domain') . '</option>';
      
    ?>
      // Get categories as array
        <?php  $args = array(
    	'taxonomy' => 'download_category', // EDD custom post type Taxonomy
        'order' => 'ASC' // Order of the list
    );
    		$categories = get_categories( $args ); ?>
    <?php
      foreach ( $categories as $category ) :
    
     // Check if current term ID is equal to term ID stored in database
     $selected = ( $stored_category_id ==  $category->term_id  ) ? 'selected' : '';
    
       echo '<option value="' . $category->term_id . '" ' . $selected . '>' . $category->name . '</option>';
    	
      endforeach;
    
    echo '</select>';
     //--This is the drop down END--
     ?>
     </div>

    This is the code that displays the results – I can’t output the result of the dropdown as $cat

    <div id="main-content" class="row store-template"> 
    <div class="content clearfix"><div class="gallery-row2">
    <?php
    $current_page = get_query_var('paged'); // Retrieving the data
    $per_page = get_option('posts_per_page');
    $offset = $current_page > 0 ? $per_page * ($current_page -1) : 0;
    			
    			
    $cat = esc_attr( $_POST[ 'cat' ]); <strong>// Is this needed?</strong>
    $product_args = array(
    	'post_type' => 'download',
    	'posts_per_page' => $per_page,
    	'offset' => $offset,
    	'download_category' => $cat[cat_name], <strong>// Change this please - it should be the output from the dropdown);</strong>
    $products = new WP_Query($product_args);
    ?>
    			
    <?php if ($products->have_posts()) : $i = 1; ?>

    And into the loop…..

    This should be so simple but it’s driving me mad!

    Help please

    • This topic was modified 6 years, 11 months ago by ageingdj.
    • This topic was modified 6 years, 11 months ago by ageingdj.
    • This topic was modified 6 years, 11 months ago by ageingdj.

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    There’s not enough information to give a complete answer. The link you provided is not working for me. Could very well be a problem on my end. The two methods you propose use different field names, so the array key containing the selected value will be different. What the rest of the form element looks like might make a difference. How the form is submitted and to where makes a difference.

    Try placing var_dump( $_REQUEST ); on your results template to see what sort of data is coming in from the browser.

    I’m not sure what your second post was supposed to be about. It looks like you thought better of it but could not delete the post. That’s fine, but FYI you should avoid follow up posts if you can because your topic then falls off of the “No Replies” list that most experts here use to find those still needing help. Follow up posts also do not alter where your topic appears in the general forum listing. So unless you need to convey essential information, I’d advise against follow up posts.

    Thread Starter ageingdj

    (@ageingdj)

    Hi there,
    Thanks for your reply.
    The link has just worked for me (mind you, I’ve been having Firefox problems for the last hour).
    I’m not sure how I got the second post! (I couldn’t work out how to delete it).
    As for the code,it returns array(0) { } – I’ve no idea what it means!
    On the two methods, all I want is to get the result of the dropdown into $cat – that’s all (I don’t care which method).

    I hope this makes sense.

    Moderator bcworkz

    (@bcworkz)

    OK, I’m getting through with your link now. The field is not within a form element and there is no submit or enter button, so the browser will not do anything with the selected data. Examples of basic forms are here. If you do not use an action attribute, the form handler code needs to be on the same page as the form.

    Thread Starter ageingdj

    (@ageingdj)

    Hi there,
    Sorry for mucking you about, trying to sort this has got my brain spinning!
    I’ve restored the page to the original template and now the drop down has a button.
    When I hit the button I get all the images from my blogs in reverse date order – I can’t figure this out.
    I looked at the tutorial already & couldn’t make it tie back to what I want (remember, I don’t speak code – I’m still learning).
    Many thanks

    Here’s the restored code:

    <?php /**
     * The template for displaying all edd products for one Category
     *
     * @package harmonic
     *
     * Template Name: TemplateEDD_ProductDropdownNew
     *
     */?>
     <?php get_header(); ?>
     <h2><div class="gallery-row"> Print Store</div></h2><br>
     
       <div class="gallery-row">
    <li id="categories">
    	<h2><?php _e( 'Categories:' ); ?></h2>
    	<form id="category-select" class="category-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
    		
    		<?php $args = array(
        'taxonomy'    => 'download_category', // EDD custom post type Taxonomy
        'order'       => 'DESC', // Order of the list
    		'value_field' => 'slug', // No idea what this does, shouldn't it be 'name'?
    			);
    		wp_dropdown_categories($args); ?>
    		
    		<input type="submit" name="submit" value="HitMe" /> 
    	</form>
    </li></div>
    
    <h5><div class="gallery-row">(Click on a button to purchase the print)</div></h5><br>
    
    			<?php
    			$current_page = get_query_var('paged'); // Is this right - what is 'paged'?
    			$per_page = get_option('posts_per_page');
    			$offset = $current_page > 0 ? $per_page * ($current_page -1) : 0;
    			$taxval = ('bike rides'); // This line now does nothing (was $taxval = get_field('selecttaxonmy');)
    			
    			$cat = esc_attr( $_GET[ 'cat' ]); //This isn't working ('cat' should be the value of the dropdown')
    			$product_args = array(
    				'post_type' => 'download',
    				'posts_per_page' => $per_page,
    				'offset' => $offset,
    				'order'  => 'DESC',
    				'download_category' => $cat, // This isn't working either (see above)
    						);
    			$products = new WP_Query($product_args);?>
    			  <?php var_dump( $_REQUEST );?>
    
    <div id="main-content" class="row store-template"> 
    		<div class="content clearfix"><div class="gallery-row2">		
    			
    			
    			<?php if ($products->have_posts()) : $i = 1; ?>
    				<?php while ($products->have_posts()) : $products->the_post(); ?>
    					<div class="threecol product<?php if($i % 4 == 0) { echo ' last'; } ?>">
    						
    												
    					
    						<a href="<?php the_permalink(); ?>">
    						<h2 class="title"><?php the_title(); ?></h2>
    					
    					
    						<div class="pauledd1">
    							<a href="<?php the_permalink(); ?>">
    								<?php the_post_thumbnail('product-image', array('class' => 'pauledd1')); ?>
    							</a></a>
    							<?php if(function_exists('edd_price')) { ?>
    								<div class="product-price">
    									
    								</div><!--end .product-price-->
    							<?php } ?>
    						</div><br><!--Space after image-->
    						<?php if(function_exists('edd_price')) { ?>
    							<div class="product-buttons">
    								<?php if(!edd_has_variable_prices(get_the_ID())) { ?>
    									<?php echo edd_get_purchase_link(get_the_ID(), 'Add to Cart', 'button'); ?>
    								<?php } ?>
    								<br><!--Space after buttons-->
    							</div><!--end .product-buttons-->
    						<?php } ?>
    					</div><!--end .product-->
    					<?php $i+=1; ?>
    				<?php endwhile; ?>
    				
    				<div class="pagination">
    					<?php 					
    						$big = 999999999; // need an unlikely intege					
    						echo paginate_links( array(
    							'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    							'format' => '?paged=%#%',
    							'current' => max( 1, $current_page ),
    							'total' => $products->max_num_pages
    						) );
    					?>
    				</div>
    			<?php else : ?>
    		
    				<h2 class="center">Not Found</h2>
    				<p class="center">Sorry, but you are looking for something that isn't here.</p>
    				<?php get_search_form(); ?>
    		
    			<?php endif; ?>
    		</div></div><!--end .content -->
    	</div><!--end #main-content.row-->
    <?php get_footer(); ?>
    Moderator bcworkz

    (@bcworkz)

    The form submit is going to a page that does not utilize your code. The URL the form requests when Ally Pally was selected is ageingdj.com/?cat=allypallly&submit=HitMe. On whatever template is used, you will find that $_GET['cat'] == 'allypallly' (note the triple l). I was curious to see what my $_REQUEST var_dump suggestion had output, so I searched for the subsequent id=”main-content” attribute in HTML source and no such attribute exists on the results page.

    Also be advised that the query argument form 'download_category' => $cat has been deprecated. AFAIK it should still work, but it will not at some point. Custom taxonomy query arguments should be using the ‘tax_query’ form.

    A few other comments in general, they do not impact your current issue:
    “Stealing” (my name for the practise) the ‘paged’ query var from the main query is unlikely to always work. It’s used for pagination purposes. The pagination of your custom query is not going to match that of the main query, so sooner or later ‘paged’ will be wrong for your query. We see all sorts of poor examples on the ‘net doing this. It is Doing it Wrong?. It’s insidious because most of the time stealing the query var does work — until it doesn’t.

    Instead of stealing the query var, you should either manage your own pagination or customise the main query through “pre_get_posts” and not do a custom query.

    You can use ‘name’ for the dropdown ‘value_field’ argument instead of ‘slug’ if you want. It needs to be coordinated with the eventual “tax_query” arguments. It’s the difference between “Ally Pally” and “allypallly”. I recommend staying with ‘slug’. It tends to be a little more consistent and predictable (when spelled appropriately ?? ).

    Thread Starter ageingdj

    (@ageingdj)

    Blimey! there’s a lot to get through there.
    I take there’s no simple way of doing what I want then?
    I thought it would be easy to pass the result of the box & button in the first part of the code (which I pinched from the Codex) into the second block of code (which I adapted from the EDD website). On their own, both code sets work, they just won’t talk to each other (at least not for me).
    Let’s hope there’s plugin to do this!

    Many thanks for everything

    Moderator bcworkz

    (@bcworkz)

    Sorry to overwhelm you. A lot of minor details to address to get it working perfectly. Really the only significant problem you’re facing is putting your code on the right template. The form needs to send the user to the page using that template. You are absolutely right — the elements need to talk to each other using the same terminology. That’s where the details come in. It’s a little beyond simple cut and paste coding.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘get_categories(): how to get output’ is closed to new replies.