• Resolved ysan

    (@ysan)


    Hello,

    This is a great plugin! Just what I was looking for.

    I’m using this shortcode

    [ic_add_posts tag='tag1,tag2']

    This results in showing posts that OR belong to tag1 OR tag2. However, I would like to show only posts that has both tag1 AND tag2.

    Is this possible?

    Kind regards,

    Yves

    https://wordpress.org/plugins/posts-in-page/

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

    (@ysan)

    Answering my own question…

    Just do it like this :

    [ic_add_posts tag='tag1' tag='tag2']

    Thread Starter ysan

    (@ysan)

    But this doesn’t seem to work for categories 🙁

    [ic_add_posts category=’cat1′ category=’cat2′]

    Does not result in all posts that belong to both Cat1 AND Cat2.

    Dear plugin authors, do you have a solution for this?

    Kind regards,

    yves

    Plugin Contributor Patrick Jackson

    (@pjackson1972)

    Hi ysan,

    Yes, as you’ve discovered, there are a number of use cases that the plugin doesn’t support right now. This includes a variety of logical expressions. For example, we don’t really have a way to express how to combine categories and tags using AND or OR operators.

    I’ve submitted a feature suggestion to add more expression into the shortcode, so hopefully we’ll be able to free up some time to work on the plugin and see some added features coming down the pipe soon!

    In the mean time, the simplest solution I can think of off hand — and there are drawbacks to this — would be to add a filter in the template. Basically, you’ll use the shortcode to get a larger list of results from the database than you want, then add a bit of code to the template to filter out the ones you don’t want.

    The drawbacks to this are (1) a small efficiency loss due to dealing with extra posts, and (2) it may mess up pagination.

    To illustrate the second point, let’s say you want 5 posts per page. Your shortcode draws 5 posts from the database, but the template filters 4 of these posts out, leaving only 1 viewable post.

    With those caveats in mind, you might give this solution a try and see if it works, but may need to manage expectations.

    If you only want to allow posts that include both tag1 AND tag2, you’ll add an if statement that uses the has_tag() function.

    Add the following line above the line that says <!-- Start of Post Wrap -->

    <?php if ( has_tag('tag1') && has_tag('tag2') ) { ?>

    In English, this line says: if the post has ‘tag1’ and it has ‘tag2’ then include the following. You can use the tag slug or tag name in the parentheses.

    At the bottom of the template, after the <!-- End of Post Wrap --> line, close the if block with another curly bracket:
    <?php } //if has tag1 & tag2 ?>

    The entire updated template would look something like this…

    <!-- NOTE: If you need to make changes to this file, copy it to your current theme's main
    	directory so your changes won't be overwritten when the plugin is upgraded. -->
    
    <?php if ( has_tag('tag1') && has_tag('tag2') ) { ?>
    <!-- Start of Post Wrap -->
    <div class="post hentry ivycat-post">
    	<!-- This is the output of the post TITLE -->
    	<h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
    	<!-- This is the output of the EXCERPT -->
    	<div class="entry-summary">
    		<?php the_excerpt(); ?>
    	</div>
    
    	<!-- This is the output of the META information -->
    	<div class="entry-utility">
    		<?php if ( count( get_the_category() ) ) : ?>
    			<span class="cat-links">
    				<?php printf( __( '<span class="%1$s">Posted in</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
    			</span>
    			<span class="meta-sep">|</span>
    		<?php endif; ?>
    		<?php
    			$tags_list = get_the_tag_list( '', ', ' );
    			if ( $tags_list ):
    		?>
    			<span class="tag-links">
    				<?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
    			</span>
    			<span class="meta-sep">|</span>
    		<?php endif; ?>
    		<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?></span>
    		<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="meta-sep">|</span> <span class="edit-link">', '</span>' ); ?>
    	</div>
    </div>
    <!-- // End of Post Wrap -->
    <?php } //if has tag1 & tag2 ?>

    I hope this helps! Let me know what you find in any case 🙂

    Plugin Contributor Patrick Jackson

    (@pjackson1972)

    By the way, Yves, thanks for posting the solution about using the tag attribute twice. I hadn’t thought to try something like that.

    I checked it out, though, and found a different behavior. In my tests, the second attribute always overrode the first one. So using your example,

    [ic_add_posts tag=’tag1′ tag=’tag2′]

    I set up a test on a clean instance with 3 posts:

    1. post 1 with only tag1
    2. post 2 with only tag2
    3. post 3 with tag1 and tag2

    The result I saw was a list populated by only the posts with tag2. The one with only tag1 was not included.

    I did a similar test using categories instead of tags and got the same behavior.

    Not sure why we saw different results, but your idea sparked my curiosity, so I thought I’d post my results.

    Thanks again for posting! It’s appreciated and encouraged! 🙂

    Thread Starter ysan

    (@ysan)

    Hello Patrick,

    It has been a while, so I do not remember the specific things I have tested. As I did not found a good solution back then, this functionality is not yet implemented in my website (however, I would really like to do it as soon as possible).

    I hope that you can find a solution and add some logical expressions. Changing the template is something I won’t do as I might need over 300 pages with different [ic_add_posts] shortcodes.

    There are actually 2 plugins with about the same functionality. Yours and List Category Posts. I prefer to use your plugin as your template system is more easy (and useful for me) (however, I would also like to suggest something about this – if you are open for suggestions on this part of the plugin).

    So in brief, I hope you can update the plugin.

    All the best,

    Yves

    Plugin Contributor Patrick Jackson

    (@pjackson1972)

    Sure, we’re always happy to hear suggestions. 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Tag1 AND Tag2’ is closed to new replies.