Conditionals if tags exist?

Try this <?php $related = new WP_Query( ‘tag=’ . get_the_title() . ” ); if( $related->have_posts() ): <h1>Related Articles</h1> while ( $related->have_posts() ) : the_post(); ?> <li><a href=”https://wordpress.stackexchange.com/questions/48774/<?php the_permalink() ?>”><?php the_title(); ?></a> </li> <?php endwhile; else: //there are no related articled endif; wp_reset_postdata();?> EDIT You should not use query_posts for a secondary loop. The query_posts alters … Read more

Exclude All Posts Which Do Not Have A Tag Assigned

here is an example loop.. you can change the number of tag to exclude based on the amount you want… <?php if (have_posts()) : while (have_posts()) : the_post(); // GET THE TAGS OF CURRENT POST IN LOOP AND COUNT THE AMOUNT $posttags = get_the_tags(); $howmany = count($posttags); if($howmany > 2) { ?> <h3><?php the_title(); ?></h3> … Read more

query_posts() – problem with “s” parameter

You shouldn’t use $_GET[‘s’] directly like that, and you definitely shouldn’t use query_posts, although I understand using it for testing. If you’re going to build your own search mechanism in this way, then you should run $_GET[‘s’]through the urldecode() function first, to make sure it doesn’t contain weird data that you might not expect and … Read more

How can I improve the performance of this query_posts loop?

Not sure how dramatic this’ll be, but it should improve nontheless: <?php /** * You can try forcibly splitting the query (grabbing IDs *then* all fields) * Works well for large post results. (available WP 3.4+) */ add_filter( ‘split_the_query’, ‘__return_true’ ); $posts = get_posts( array( ‘post_type’ => ‘listings’, ‘posts_per_page’ => -1, /* Disable term caching … Read more

how to store wordpress loop in array?

Use get_posts instead – You can use the function get_posts() to get all the posts as array. This function accepts almost all parameters that of query post’s. Example – $args = array( ‘numberposts’ => 3,’category’ => 3 ); $homePost = get_posts( $args ); $one = $homePost[0]; $two = $homePost[1]; $three = $homePost[2]; //print_r($one); // lets … Read more

How do I rewrite this loop as a new WP_Query style-loop?

You’re 95% of the way there already. Just add if ( $my_query->have_posts() ) and incorporate your $odd variable: <?php $my_query = new WP_Query(‘offset=5&showposts=10’); if ( $my_query->have_posts() ) : $odd = false; while ($my_query->have_posts()) : $my_query->the_post(); $odd = !$odd; ?> <div class=”<?php if ($odd) echo ‘uneven ‘; ?>post”> <h3><a href=”https://wordpress.stackexchange.com/questions/70713/<?php the_permalink(); ?>”><?php the_title(); ?></a></h3> <?php the_excerpt(); … Read more