Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm transitioning all my query_posts queries to get_posts after a lot of research about how bad it is for performance. My solution is get_posts but working with it is confusing me. Here is what I have:

    $posts = get_posts('showposts=-1&offest=10&post_type=any');
            foreach ($posts as $post) :
                ?>
                    <div class="box">
                    <a href="<?php the_permalink() ?>" rel="bookmark">
                    <h2><?php the_title(); ?></h2>
                    <?php if (has_post_thumbnail()) {
                        the_post_thumbnail('thumbnail');
                    }?>
                    <?php the_content();?></a>
                    </div><?php endforeach; ?>

It works but some of the params don't, such as offset and "any" post type is to vague.

$posts = get_posts('showposts=-1&offest=10&post_type=any');

The codex tells us that we can also use an array:

$query = new WP_Query( array( 'post_type' => array( 'post', 'page', 'movie', 'book' ) ) );

But this is a common problem I don't know how to handle. How do I get dynamic parameters into the array, or where can I find the rules for parameters in string format like my code?

share|improve this question
    
what you do mean by dynamic parameters? –  Reigel Jan 29 '13 at 1:09
    
you've typed in offest... maybe that's why it's not working... –  Reigel Jan 29 '13 at 1:29
add comment

2 Answers

up vote 1 down vote accepted

How do I get dynamic parameters into the array,...

This example from the Codex demonstrates that:

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$sticky = get_option( 'sticky_posts' );
$args = array(
    'cat' => 3,
    'ignore_sticky_posts' => 1,
    'post__not_in' => $sticky,
    'paged' => $paged
);
$query = new WP_Query( $args );

Both $paged and $sticky are variables-- I assume that is what you mean by 'dynamic parameters'.

or where can I find the rules for parameters in string format like my code?

Not entirely sure what this means but the Codex page for WP_Query should give you all the parameters. You seem to know how to create that query-string-like string, but I encourage you not to. Build an array and use that. That string is hard to read. It is hard to edit. And it probably doesn't work with the more complicated queries, like the meta_query, but on that I am really not sure since I never, ever use that string syntax.

share|improve this answer
    
you can use variables within your array! Thanks s_ha_dum. I will stick to arrays from now on. –  BenRacicot Jan 29 '13 at 2:31
add comment

I figured it out and thought I'd post the answer to this question for indexed arrays after the sweet explanation by @s_ha_dum for associative arrays:

You have to loop through each value in an array. For instance with add_meta_box() the function must be fired FOREACH custom post type

function add_post_meta_boxes() {
  $types = array('custom_type', 'post', 'page');
     foreach( $types as $type ) {
        add_meta_box( 
            'unique_id',    // Unique ID
            esc_html__( 'Post Class', 'themename' ), // Title
            'CALLBACK_FUNC', // Callback function - html
            $type, // Admin page (or post type)
           'side',  // Context
           'default' // Priority
        );
     }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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