• Hi all,
    this is the goal:
    filter some posts, added in a category, by a custom field and with the right pagination.

    The custom field is a value that I get through a form (dropdown menu in a sidebar with custom field values) and passed via php.
    The custom query post code is in the archive.php file.

    This is the little code:

    <?php	$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("cat=12&meta_key=$cfname&meta_value=$cfvalue&paged=$page"); ?> 
    
    cfname = the custom field name
    cfvalue = the custom field value

    I get the filtered posts correctly but the pagination doesn’t work.

    Here’s the problem:
    the link “Next page” points to /category/page/2/ and it’s wrong. If I click I get the normal page 2 with ALL posts listed in that category but I need the page 2 with the posts filtered before.

    Any help or new idea pls? ??

    Thanks,
    ck

Viewing 7 replies - 1 through 7 (of 7 total)
  • If you add &posts_per_page=5 to your query_posts arguments does that work. Also assume you have previous_posts_link() and next_posts_link() in your template.

    Thread Starter cl3rik

    (@cl3rik)

    Thanks for response.

    I’ve added that argument but it doesn’t work. The previous_posts_link() and next_posts_link() are at the end of the archive.php

    In the sidebar I tried both
    <form action="<?php bloginfo('url'); ?>/category/projects/" method="post">

    and

    <form action="<?php bloginfo('url'); ?>/" method="post">

    Still nothing.

    Thanks
    ck

    Then paste the entire content of that template in a pastebin and report the link back here. Maybe someone can spot the problem.

    Thread Starter cl3rik

    (@cl3rik)

    Here’s the code:

    <?php get_header(); ?>
    
    <div id="content" class="bg_sidebar">
    
                <div id="inner_content">
    
    				<span class="meta"><?php echo $punchline; ?></span>
    
    				<?php if (isset($_POST['year']) || isset($_POST['genre'])) { ?>
    
    							<?php if (isset($_POST['year'])) { $valore_variabile=$_POST['year']; $nome_variabile="year"; }
    								  if (isset($_POST['genre'])) { $valore_variabile=$_POST['genre']; $nome_variabile="genre"; } ?>
    
    							<h2><?php echo $valore_variabile; ?> projects</h2>	
    
    	                        <?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    							query_posts("cat=12&posts_per_page=1&meta_key=$nome_variabile&meta_value=$valore_variabile&paged=$page"); ?>
    
    							<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    		                                <div class="entry-project" onclick="window.open('<?php the_permalink() ?>', '_self');">
    
    		                                	<?php $cover = get_post_meta($post->ID, "cover", true); 
    
    		                                    	if( $cover != "" )
    		                                        	{
    		                                            	$resizepath = get_bloginfo('template_url')."/timthumb.php?src="; #timthumb path
    		                                                $resize_options1 = "&w=50&h=70&zc=1";
    		                                                $cover = $resizepath.$cover.$resize_options1;
    													}
    											?>
    
    other code, just some get post meta etc etc...
    		                                    <br clear="all" />
    										</div>
    
    							<?php endwhile;?>
       							<?php endif; ?>
    				<?php } else { ?>
    
    				<h2>Recent <?php single_cat_title(); ?> projects</h2>
    
                	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    					<div class="entry-project" onclick="window.open('<?php the_permalink() ?>', '_self');">
    
                    <?php $cover = get_post_meta($post->ID, "cover", true); 
    
    						if( $cover != "" )
    						{
    							$resizepath = get_bloginfo('template_url')."/timthumb.php?src="; #timthumb path
    							$resize_options1 = "&w=50&h=70&zc=1";
    
    							$cover = $resizepath.$cover.$resize_options1;
    						}
    
    				?>
    
                           other code, just some get post meta etc etc...
    
                            <br clear="all" />
    					</div>
    
    			<?php endwhile; ?>
    			<?php endif; ?>
    			<?php } ?>
    
                     <div class="pagination">
                       <?php posts_nav_link(); ?>
    				</div>
    
                </div>
    
     		<?php get_sidebar('projects'); ?>     
    
    </div><!-- end content-->
    
    <?php get_footer(); ?>

    You’ve got two loops there and not sure that works with paging.

    Might try a wp_reset_query(); before the 2nd loop.

    Thread Starter cl3rik

    (@cl3rik)

    Nothing, same problem. As you said, maybe that works with one loop only.

    Any idea how to manage this goal? maybe with a plugin? any tip is welcome ??

    thanks
    ck

    Thread Starter cl3rik

    (@cl3rik)

    Hi,
    I tried coding the archive page with one loop only and seems work but

    ONLY if I set in the query post the meta key and the meta value “manually”. If I set them throught 2 variables passed via POST it doesn’t work. ??

    Above the loop I tried this code:

    <?php	/*if (isset($_POST['year'])) 
    
    	{
    
    		$meta_key="year";
    		$meta_value=$_POST['year'];*/
    
    		$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		query_posts("cat=12&posts_per_page=1&meta_key=$meta_key&meta_value=$meta_value&paged=$page");
    
    	}
    ?>

    Then Loop:
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    Then some HTML/PHP code…

    Then:

    <?php endwhile; ?>
    <?php endif; ?>
    <?php wp_reset_query(); ?>

    Thanks
    ck

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Filter by custom field and pagination’ is closed to new replies.