0

my situation: I have a frontpage which filters 12 thumbnails out of 24 (each thumbnail represents a post's featured image), and displays them. The user has the ability to hide whichever post he wants from the frontpage.

Let's say the user chooses to hide thumb #4, that means we now have thumb 1,2,3,5,6,7,8,9,10,11,12. That is, we have 11 thumbnails. So we are missing the last 'thumbnail space', which should be filled with the upcoming post; in this case, thumb #13.

In short, the frontpage should query the posts so that when one or more thumbnails are hidden - resulting in one or more blank spaces - it automatically 'refills' the blank spaces by pushing in the upcoming thumbnails.

My front-page has this query:

            <?php 
            global $wp_query;

            // First row of images
            if(!empty($options['home_thumbs'])) {
                $page_items = $options['home_thumbs'];
            } else {
                $page_items = 18;
            }

            $portCat = get_category_id($options['portfoliocat']);

            // get the desired sort order of portfolio items
            if($options['homepage_sort'] == 'DESC') { $order = 'DESC'; } else { $order = 'ASC'; }

            $hideFromHome = get_post_meta($wp_query->post->ID, 'pr_hidehome', true);

            query_posts('posts_per_page=' . $page_items . '&orderby=title&order=' . $order . '&cat=' . $portCat . '&meta_key=' . $hideFromHome .'&meta_value=' . true);
            // arrays to detect first and last columns
            $firsts = array(6,12,18,24,30,36);
            $lasts = array(5,11,17,23,29,35);
            if (have_posts()) :
                $count = 0;
                echo '<ul class="imageRows rowOne">';
                while (have_posts()) : the_post();
                $custom_meta = get_post_custom($post->ID);

                    if (has_post_thumbnail() && $custom_meta['pr_hidehome'][0] != true) { ?>
                        <li class="<?php if (in_array($count, $firsts) ) { echo ' columnFirst'; } if (in_array($count, $lasts) ) { echo ' columnLast'; } ?>">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                <?php
                                    $atts = array(
                                        'class' => "attachment-$size imageBlock",
                                        'alt'   => get_the_title(),
                                    );
                                    the_post_thumbnail('portfolioSmall', $atts); 

                                ?>
                            </a>
                        </li>                   
                        <?php
                        $count++;
                    }
                endwhile; 
                echo '</ul> <!-- First row -->';
                else :          
                echo '<h3>Oops, something went wrong.</h3>';
             endif;

        ?>                                              

pr_hidehome is the option that, when enabled, hides the respective post (but leaves that empty space I'd need to be filled in with one or more of the upcoming thumbnails). You can also see how I am trying to query the posts. However, on this article, I read that the best way to query posts by custom fields is using an array with 'meta_query' in it. So here's what I tried it:

            <?php 
            global $wp_query;

            // First row of images
            if(!empty($options['home_thumbs'])) {
                $page_items = $options['home_thumbs'];
            } else {
                $page_items = 18;
            }

            $portCat = get_category_id($options['portfoliocat']);

            // get the desired sort order of portfolio items
            if($options['homepage_sort'] == 'DESC') { $order = 'DESC'; } else { $order = 'ASC'; }


            $args = array(
            'posts_per_page' => $page_items,
            'orderby' => 'title',
            'order' => $order,
            'cat' => $portCat,
            'meta_query' => array(
                    array(
                        'key' => 'pr_hidehome',
                        'value' => 'the_value_you_want',
                        'compare' => 'LIKE'
                    )
                )
            );
            query_posts($args);

            // arrays to detect first and last columns
            $firsts = array(6,12,18,24,30,36);
            $lasts = array(5,11,17,23,29,35);
            if (have_posts()) :
                $count = 0;
                echo '<ul class="imageRows rowOne">';
                while (have_posts()) : the_post();
                $custom_meta = get_post_custom($post->ID);

                    if (has_post_thumbnail() && $custom_meta['pr_hidehome'][0] != true) { ?>
                        <li class="<?php if (in_array($count, $firsts) ) { echo ' columnFirst'; } if (in_array($count, $lasts) ) { echo ' columnLast'; } ?>">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                <?php
                                    $atts = array(
                                        'class' => "attachment-$size imageBlock",
                                        'alt'   => get_the_title(),
                                    );
                                    the_post_thumbnail('portfolioSmall', $atts); 

                                ?>
                            </a>
                        </li>                   
                        <?php
                        $count++;
                    }
                endwhile; 
                echo '</ul> <!-- First row -->';
                else :          
                echo '<h3>Oops, something went wrong.</h3>';
             endif;

        ?>                                              

Sadly this gave me the fallback error 'Oops, something went wrong'.

That's it. I hope I explained myself well enough. If not, I'll try and rephrase everything so that you can help me out!

Thank you very much. All help is greatly appreciated ;)

2
  • Ehm, what is your actual question here? :)
    – Rarst
    Commented Apr 7, 2011 at 19:46
  • Wow, sorry.. I focused so much on making this understandable that I forgot to actually ask the real question. I've edited the post now. Basically, I'd like to know what I'm doing in the second snippet I pasted.
    – cr0z3r
    Commented Apr 7, 2011 at 19:59

2 Answers 2

0

Ok, this is your original meta conditions that describe that you want all posts where key with name from $hideFromHome equals (meta_compare is = by default) true (actually 1 since you concatenate boolean value with string).

'&meta_key=' . $hideFromHome .'&meta_value=' . true

What you got in second snippet defines key name as pr_hidehome, value as string the_value_you_want and explicit comparison as LIKE (which is SQL term and acts like somewhat tricky version of string search):

'meta_query' => array(
                    array(
                        'key' => 'pr_hidehome',
                        'value' => 'the_value_you_want',
                        'compare' => 'LIKE'
                    )
                )

To express you original conditions with newer syntax it would be something like this:

'meta_query' => array(
                        array(
                            'key' => $hideFromHome,
                            'value' => true,
                        )
                    )

Again, = is implied by default, but you might also need type to make comparison with boolean work, depending on what your custom field holds.

See Custom Field Parameters in Codex for full list of available stuff.

3
  • Hey thanks, I did this and it doesn't show that 'Oops, something..' error anymore. But, it doesn't fill the thumb #13 gap either. Have a look: crozer.me/wp/practica
    – cr0z3r
    Commented Apr 7, 2011 at 20:26
  • Anyway, I found the solution. There had to be a few mods done. If anybody needs the fixed code, let me know.
    – cr0z3r
    Commented Apr 8, 2011 at 4:26
  • 1
    @cr0z3r if you came up with solution by yourself it is good practice to add it as an answer and accept it so others can benefit from it.
    – Rarst
    Commented Apr 8, 2011 at 7:56
0

Will do. It's just a bit more complicated, but here it goes:

<?php
/**
 * WP Practica
 * Home Page Template
 */

get_header(); ?>
<?php $options = get_option( 'practica_theme_settings' ); ?>
<div id="main"> <!-- start div#main -->

    <!-- Optional 3D style --><div class="dimWrap_content"></div>
    <div id="content" > <!-- start div#content -->

        <?php 
            global $wp_query;

            // First row of images
            if(!empty($options['home_thumbs'])) {
                $page_items = $options['home_thumbs'];
            } else {
                $page_items = 18;
            }

            $portCat = get_category_id($options['portfoliocat']);

            // get the desired sort order of portfolio items
            if($options['homepage_sort'] == 'DESC') { $order = 'DESC'; } else { $order = 'ASC'; }

            $hideFromHome = get_post_meta($wp_query->post->ID, 'pr_hidehome', true);

            query_posts('posts_per_page=-1&orderby=title&order=' . $order . '&cat=' . $portCat . '&meta_key=' . $hideFromHome .'&meta_value=' . true);
            // arrays to detect first and last columns
            $firsts = array(6,12,18,24,30,36);
            $lasts = array(5,11,17,23,29,35);

            if ( $options['nothumb_content'] == true ) { ?>
            <div id="page-content">
                <?php the_content(); ?>
            </div>
            <?php } else {
            if (have_posts()) :
                $count = 0;
                echo '<ul class="imageRows rowOne">';
                while (have_posts()) : the_post();
                $custom_meta = get_post_custom($post->ID);

                    if (has_post_thumbnail() && $custom_meta['pr_hidehome'][0] != true) { ?>
                        <li class="<?php if (in_array($count, $firsts) ) { echo ' columnFirst'; } if (in_array($count, $lasts) ) { echo ' columnLast'; } ?>">
                        <?php if ($custom_meta['pr_openpretty_home'][0] != true) { ?>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                <?php
                                    $atts = array(
                                        'class' => "attachment-$size imageBlock",
                                        'alt'   => get_the_title(),
                                    );
                                    the_post_thumbnail('portfolioSmall', $atts); 

                                ?>
                            </a>
                        <?php } else {
                            if ($custom_meta['pr_openpretty_video'][0] == 'Your URL' || $custom_meta['pr_openpretty_video'][0] == '') { ?>
                                <a href="<?php $image_id = get_post_thumbnail_id();
                                        $image_url = wp_get_attachment_image_src($image_id,’large’, true);
                                        echo $image_url[0]; ?>" title="<?php the_title_attribute(); ?>" rel="prettyPhoto<?php 
                                        if(!empty($custom_meta['pr_openpretty_slideshow'][0])) { 
                                            echo '[';
                                            echo $custom_meta['pr_openpretty_slideshow'][0];
                                            echo ']';
                                        } ?>">
                                    <?php
                                        $atts = array(
                                            'class' => "attachment-$size imageBlock",
                                            'alt'   => get_the_title(),
                                        );
                                        the_post_thumbnail('portfolioSmall', $atts); 

                                    ?>
                                </a>
                                <?php } else { ?>
                                <a class="videoBlock" href="<?php echo $custom_meta['pr_openpretty_video'][0]; ?>?width=100%&height=100%" title="<?php the_title_attribute(); ?>" rel="prettyPhoto<?php 
                                        if(!empty($custom_meta['pr_openpretty_slideshow'][0])) { 
                                            echo '[';
                                            echo $custom_meta['pr_openpretty_slideshow'][0];
                                            echo ']';
                                        } ?>">
                                    <?php
                                        $atts = array(
                                            'class' => "attachment-$size imageBlock",
                                            'alt'   => get_the_title(),
                                        );
                                        the_post_thumbnail('portfolioSmall', $atts); 

                                    ?>
                                </a>
                                <?php } ?>
                        <?php } ?>

                        </li>                   
                        <?php
                        $count++;
                        if($count == $page_items) { break; }
                    }
                endwhile; 
                echo '</ul> <!-- First row -->';
                else :          
                echo '<h3>Oops, something went wrong.</h3>';

             endif;
            }
        ?>                                              
    </div> <!-- end div#content -->

<?php get_footer(); ?>

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.