0

I created a search for my site using attachments. Each attachment is in category "Pictures", so my search form looks like:

<form method="get" id="searchform" action="<?php bloginfo('url'); ?>">
    <input type="text" name="s" id="s" value="Search..." onfocus="if (this.value=='Search...') this.value='';" onblur="if (this.value=='') this.value='Search...';" />
    <?php
        $parent = get_cat_ID("Pictures");
    ?>
    <input type="hidden" name="cat" value="<?php echo $parent; ?>" />
    <input type="submit" id="searchsubmit" value="" />
</form>

My search results page search.php returns those results through a get_postsquery. The problem I am having is with the pagination. I have been able to limit the number of posts on the page and even return the pagination, but when I click on the link for the pagination, the page does not display. I am thinking because these are attachments the pages are setup a little different, but I don't know what the url looks like. My current code looks like:

<?php
    $args = array(
        'post_type' => 'attachment',
        'numberposts' => '-1',
        'category_name' => get_the_title()
    );
    $images = get_posts($args);
    if (!empty($images)) {
?>

    <?php
        $limit = 5;

        $total = count($images);
        $pages = ceil($total / $limit);
        $result = ceil($total / $limit);

        $current = isset($_GET['paged']) ? $_GET['paged'] : 1;
        $next = $current < $pages ? $current + 1 : null;
        $previous = $current > 1 ? $current - 1 : null;

        $offset = ($current - 1) * $limit;
        $images = array_slice($images, $offset, $limit)
    ?>

    <h4><span>Search</span> Results</h4>
    <ul class="category">
        <?php
            foreach ($images as $image) {
                $title = $image->post_title;
                $description = $image->post_content;
                $attachment_link = get_attachment_link( $image->ID );
        ?>
            <li>
                <div class="col1">
                    <a href="<?php echo $attachment_link; ?>"><?php echo wp_get_attachment_image($image->ID, "medium"); ?></a>
                </div>
                <div class="col2">
                    <h5><?php echo $title; ?></h5>
                    <p><?php echo $description; ?></p>
                </div>
            </li>
        <?php } ?>
    </ul>

    <?php echo "<p>(Page: ". $current . " of " . $result .")</p>"; ?>
    <? if($previous): ?>
        <a href="<?php bloginfo('url'); ?>?paged<?= $previous ?>">Previous</a>
    <? endif ?>
    <? if($next) : ?>
        <a href="<?php bloginfo('url'); ?>?paged<?= $next ?>">Next</a>
    <? endif ?> 
<?php } ?>

The paging was integrated from the code I found on: https://erikeldridge.wordpress.com/2009/01/11/simple-php-paging/

I'm so close, probably just a tweak here or there, can anyone point me in the right direction?

Thanks,
Josh

2 Answers 2

0

Your problem is that you use core url parameters (paged in this case) for something that is not the main loop.

There are two possible solutions

  1. instead of writing your own loop, use the pre_get_posts filter to modify the main loop.

  2. Use your own pagination parameters (for example jr_paged).

6
  • Hey there, I don't mind using either of those, on #1 I don't know what kind of filter I should be looking for, I am guessing a "Include Custom Post Types in Search Results" from codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts? Jul 26, 2017 at 12:38
  • Also, for #2, what would the url look like? I changed my paging parameters and that didn't work: $current = isset($_GET['jr_paged']) ? $_GET['jr_paged'] : 1; and <a href="<?php bloginfo('url'); ?>?jr_paged/<?= $previous ?>">Previous</a> Jul 26, 2017 at 12:39
  • hmm the second option is more complex to implement as a "pretty" urls. you should probably try the first option first using the filter.... not finding any good example I will just link google.co.il/… to what might be a good place to start learning how to use that filter Jul 26, 2017 at 15:57
  • Unfortunately, I've been trying to integrate get_posts without success. I think one of the main obstacles is I am trying to get everything from the attachment post type, but I don't have actual posts created...all the data that's returned is from categories that are assigned to the attachments themselves...so (essentially) there are no posts to page...is there a way to make the search results have paging but just return attachments or maybe have it use the attachment template? Jul 28, 2017 at 13:20
  • not sure why do you use get_posts Jul 28, 2017 at 13:25
0

I found a solution!

I installed the "Search Everything" plugin (https://wordpress.org/plugins/search-everything/) and in the plugin settings I selected to search all categories and all attachments.

Then, on my search results page I query the posts with the paging parameter. I needed the plugin to search categories, because each image in the Media Library has categories, but that would return posts from all categories, it would not just be limited to attachments. So, in my query, I made sure that the attachment post type was specified.

The code now looks like:

<?php
    $paged = (get_query_var("paged")) ? get_query_var("paged") : 1;
    query_posts($query_string."&post_type=attachment&posts_per_page=10&paged=".$paged);
    $count = $wp_query->found_posts; 
?>
    <h4><span>Search</span> Results</h4>
    <p align="center">We were able to find <?php if ($count == 1) $count .= " Image"; else $count .=" Images"; echo $count; //Image Count ?> for: "<?php the_search_query(); ?>"</p>
    <?php if ( have_posts() ) : ?>
        <ul class="category">
            <?php while ( have_posts() ) : the_post(); ?>
                <li>
                    <div class="col1">
                        <a href="<?php the_permalink(); ?>"><?php echo wp_get_attachment_image($image->ID, "medium"); ?></a>
                    </div>
                    <div class="col2">
                        <h5><?php the_title(); ?></h5>
                        <?php
                            $thumb = get_post(get_post_thumbnail_id()); // Get post by ID
                            echo '<p>'.$thumb->post_content.'</p>'; // Display Description
                        ?>
                    </div>
                </li>
            <?php endwhile; ?>
        </ul>
    <?php endif; ?>
    <?php echo "<p>(Page: ". $paged . " of " . $wp_query->max_num_pages .")</p>"; ?>
    <p><?php previous_posts_link( 'Previous' ); ?></p>
    <p><?php next_posts_link( 'Next', '' ); ?></p>
<?php wp_reset_query(); ?>

Not sure what "Search Everything" is doing differently, but this works and has the right functionality. I would have loved to have used something besides a plugin, but this gets the job done :-)

Thanks,
Josh

Your Answer

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

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