0

I have over 1000 posts with no tags. Basically I am trying to get posts with no tags to show in frontend so that users can add tags from frontend.

I am using this approach in loop so as to show posts with no tags.

<?php
    $tag = get_the_tags();
    if (! $tag) { ?>
        <a href="<?php the_permalink();?>"><?php the_title() ?></a><br>
    <?php }
?>  

But is there a way so I can use WP_Query() itself so as to filter because $args are not applying properly with the above approach.

Thanks.

Full Code -

<?php 

$args = array(
    'post_type'  => 'post',
);

$the_query = new WP_Query($args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <?php
            $tag = get_the_tags();
            if (! $tag) { ?>
                <a href="<?php the_permalink();?>"><?php the_title() ?></a><br>
            <?php }
         ?>         
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php wp_reset_query(); ?>

1 Answer 1

1

I'm not in the position to code right now, but here is an idea

A thousand posts is quite a lot, but it is even more if you are retrieving all posts from the db in one go. What if you have ten thousand posts, of which only ten or twenty is not tagged after the whole retagging operation. This can become such a huge query that you run the danger of timing out

I would suggest the following:

  • If this operation is just going to be used for the purpose of retagging only without paging, use get_posts. WP_Query returns a lot of information that won't be necessary.

  • Retrieve a manageable amount of tags at any given time, say 100 tags at a time till it is all done. This will not only load faster, but your list will also not be that overwhelming. You can also paginate your list if you wish, but again, as not to overwhelm the user, I would not do this. Stick to one page.

To properly manage and retrieve only the posts without tags, you will first need to get a list of all your tags. This can simply be done by the function get_tags(). You most probably will need to get only the ids from the returned array of tag objects. For this you can make use of wp_list_pluck

This can then be used in your query to exclude all posts which have tags attached to them. You can make use of the tag__not_in parameter. This will only retrieve posts now which does not any tags

Here is a concept query (Caveat: Untested and as stated, only a concept)

$tags = get_tags();
$tag_ids = wp_list_pluck( $tags, 'term_id' );

$args = array(
   'posts_per_page' => 100,
   'tag__not_in' => $tag_ids
);

$query = get_posts( $args ); // Use which suits your best, get_posts or WP_Query
// $query = new WP_Query( $args );

// YOUR POST LOOP
2
  • Thank You. It worked well. I ended up paginating the list as suggested. Commented Nov 10, 2014 at 4:34
  • I'm glad it worked for you. Enjoy :-) Commented Nov 10, 2014 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.