• Luigino

    (@luigino)


    Hello everyone,

    I have a template-based page where I want to show posts of a certain category and filtering them by a tag they have so I can exclude some posts.
    The code is this, as you see I have ‘tag’=>’rassegna_stampa’ in $args:

    <?php
    if (is_page_template()) :
    $paged = ( get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
    $args=array(
    ‘category__in’=>array(24),
    ‘posts_per_page’=>6,
    ‘caller_get_posts’=>1,
    ‘tag’=>’rassegna_stampa’,
    ‘paged’=>$paged
    );

    query_posts($args);
    print_r($wp_query);
    endif;
    ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();
    echo ‘<div class=”category_description”>’; if ( in_category(’24’) ) the_content(); echo ‘</div>’;
    endwhile; ?>

    <div class=”navigation”>
    <span class=”previous-entries”><?php next_posts_link(Previous entries…’) ?></span><span class=”next-entries”><?php previous_posts_link(Next entries…’) ?></span>
    </div>

    <?php else : ?>
    <span class=”center”>Sorry, no posts in this page.</span>
    <?php endif; ?>
    And the result of print_r($wp_query) is this:

    WP_Query Object ( [query_vars] => Array ( [category__in] => Array ( [0] => 24 ) [posts_per_page] => 6 [caller_get_posts] => 1 [tag] => rassegna_stampa [paged] => 1 [error] => [m] => 0 [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [hour] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [cat] => [tag_id] => 59 [author_name] => [feed] => [tb] => [comments_popup] => [meta_key] => [meta_value] => [preview] => [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( [0] => rassegna_stampa ) [tag_slug__and] => Array ( ) [suppress_filters] => [post_type] => post [nopaging] => [comments_per_page] => 50 [order] => DESC [orderby] => bin09_posts.post_date DESC ) [request] => SELECT SQL_CALC_FOUND_ROWS bin09_posts.* FROM bin09_posts INNER JOIN bin09_term_relationships ON (bin09_posts.ID = bin09_term_relationships.object_id) INNER JOIN bin09_term_taxonomy ON (bin09_term_relationships.term_taxonomy_id = bin09_term_taxonomy.term_taxonomy_id) INNER JOIN bin09_terms ON (bin09_term_taxonomy.term_id = bin09_terms.term_id) WHERE 1=1 AND bin09_term_taxonomy.taxonomy = ‘category’ AND bin09_term_taxonomy.term_id IN (’24’) AND bin09_term_taxonomy.taxonomy = ‘post_tag’ AND bin09_terms.slug IN (‘rassegna_stampa’) AND bin09_posts.post_type = ‘post’ AND (bin09_posts.post_status = ‘publish’) GROUP BY bin09_posts.ID ORDER BY bin09_posts.post_date DESC LIMIT 0, 6 [post_count] => 0 [current_post] => -1 [in_the_loop] => [post] => [comments] => [comment_count] => 0 [current_comment] => -1 [comment] => [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => 1 [is_tag] => 1 [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_404] => [is_comments_popup] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_paged] => 1 [query] => Array ( [category__in] => Array ( [0] => 24 ) [posts_per_page] => 6 [caller_get_posts] => 1 [tag] => rassegna_stampa [paged] => 1 ) [posts] => Array ( ) ) Sorry, no posts in this page.

    But it doesn’t show any post, even the message “Sorry, no posts in this page”. Indeed, if I remove ‘tag’=>’rassegna_stampa’ from $args and put has_tag(‘rassegna_stampa’) inside: if ( in_category(’24’) ) the_content(); it shows 6 posts only at first page, and 4 posts in the second and 2 posts in the third; plus at last page it still show the link previous entries when it shouldn’t.
    I suspect it’s just extracting ALL posts but simply without showing those that doesn’t have the tag ‘rassegna_stampa’ but effectively for the Loop they are 6 per page.

    So I am asking you how I can filter those posts by tag in the $args correctly so I can see posts?…

    Thanks to everyone in advance!!
    Ciao
    Luigi

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Luigino

    (@luigino)

    Uhm… no clue?…

    Thread Starter Luigino

    (@luigino)

    I have a category with id=24 and some posts that have different tags with ID=59,44,12 and I’d like to extract posts from that category filtering them getting only id=59… so I did:
    $args=array(
    ‘category__in’=>array(24),
    ‘posts_per_page’=>6,
    ‘caller_get_posts’=>1,
    ‘tag_id’=>59,
    ‘paged’=>$paged
    );

    query_posts($args);

    then I load the page but it extracts all the posts of that category… why?… I tried also with ‘tag__in’=>array(59) but same result…. I cheched if I could have wrong id of that tag but it’s right….
    At least what I can check to see if there’s something wrong, like in database?…
    Pratically I would understand how it works this tag_id/tag__in in the core of wordpress just to check what could be wrong….

    any suggest would be appreciated thanks!!! ??

    Ciao
    Luigi

    Thread Starter Luigino

    (@luigino)

    Hello again everyone…..

    I tried to modify with this code:

    if (is_page_template()) :
    	$pageposts = $wpdb->get_results("SELECT p.*
    									 FROM $wpdb->posts p, $wpdb->terms t, $wpdb->term_taxonomy tt,
    										  $wpdb->term_relationships tr, $wpdb->terms t2, $wpdb->term_taxonomy tt2,
    										  $wpdb->term_relationships tr2, $wpdb->postmeta meta
    									 WHERE p.id = tr.object_id
    									   AND p.id = meta.post_id
    									   AND p.post_status = 'publish'
    									   AND t.term_id = tt.term_id
    									   AND tr.term_taxonomy_id = tt.term_taxonomy_id
    									   AND p.id = tr2.object_id
    									   AND t2.term_id = tt2.term_id
    									   AND tr2.term_taxonomy_id = tt2.term_taxonomy_id
    									   AND (tt.taxonomy = 'category' AND tt.term_id = t.term_id AND t.slug = 'category-description')
    									   AND (tt2.taxonomy = 'post_tag' AND tt2.term_id = t2.term_id AND t2.slug = 'rassegna_stampa')", OBJECT);
    endif;
    ?>
    <?php if ($pageposts) : foreach ($pageposts as $post) : setup_postdata($post);
    	echo '<div class="category_description">'; if ( in_category('24') ) the_content(); echo '</div>';
    endforeach; ?> 
    
    <div class="navigation">
    	<span class="previous-entries"><?php next_posts_link('Articoli precedenti...') ?></span><span class="next-entries"><?php previous_posts_link('Articoli successivi...') ?></span><br />
    </div>
    
    <?php else : ?>
    	<span class="center">Mi dispiace, ma non sono presenti articoli in questa pagina.</span>
    <?php endif; ?>

    which it works about filtering correctly…. but how I can add paging showing 6 posts per page and showing links “next entries” and “previous entries”?….

    Thanks everyone in advance for your little help!!!
    Ciao
    Luigi

    Thread Starter Luigino

    (@luigino)

    No suggests/ideas?….

    Thanks in advance to everyone
    Ciao
    Luigi

    Thread Starter Luigino

    (@luigino)

    Up…

    Thread Starter Luigino

    (@luigino)

    Nevermind…. solved!
    Thanks anyway

    Ciao!
    Luigi

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Filtering posts in a template-based page’ is closed to new replies.