WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a shortcode where I'm trying to get a specific string from the $args array below.

Query:

$args = array(
    //'post_type' => $posttype,
    'post_type' => explode( ', ', $posttype ),
);    
$myquery = new WP_Query( $args );

Conditional:

if ( $posttype == 'cpt_press' ) :
    the_content();
else : 
    the_excerpt();
endif;

Shortcode:

[myquery posttype='cpt_press']

In the conditional above, I'm able to retrieve all posts with post type 'cpt_press' if I don't use explode. The reason I used explode is so I could do this:

[myquery posttype='cpt_press, cpt_two, cpt_three, cpt_etc']

Any help?

UPDATED CODE BLOCK

function myshortcode( $params, $content = null ) {
    global $post;
    extract( shortcode_atts( array(
        'posttype'      => '',
        'meta_key'      => '',
        'priority'      => '',
        'meta_compare'  => '',
        'layout'        => 'rows',
        'cols'          => 1, 
        'tag'           => '',
        'count'         => 10, 
        'orderby'       => 'date',
        'order'         => 'DESC'
    ), $params ) );   
    $args = array(
        'post_type' => explode( ',', $posttype ),
     );    
    $myquery = new WP_Query( $args );  
    ob_start();       
    ?><div class="row"><?php    
    // The Loop
    if ( $myquery->have_posts() ) : while( $myquery->have_posts() ) : $myquery->the_post();    
        if ( $posttype == 'cpt_press' ) :
            the_content();   
        else :
            the_excerpt();
        endif;
    endwhile; 
    endif;
    wp_reset_postdata();
    ?></div><?php 
    return ob_get_clean(); 
}    
add_shortcode( 'myquery', 'myshortcode' );    
share|improve this question
    
Post the code in context please, such that it can seen as a whole and pasted into a dev stack if necessary. As it is, I can't really tell what you are doing. – s_ha_dum Mar 14 '14 at 16:51
    
@tai-sem, Did you mean how to tell if 'cpt_press' is in the exploded string? You could use if ( in_array( 'cpt_press', $posttype ) ). – 1fixdotio Mar 14 '14 at 17:36
    
@1fixdotio Thanks I've already tried in_array :( – Tai Sem Mar 14 '14 at 17:43
    
@s_ha_dum It's a basic shortcode plugin. You really need all the code? I can edit the question, but I'm searching for: how to grab a string from explode array. – Tai Sem Mar 14 '14 at 17:46
    
Have you var_dump the "$posttype" and make sure it is an array with "cpt_press" in it? – 1fixdotio Mar 14 '14 at 17:46

Please check the following code:

<?php
function myshortcode( $params, $content = null ) {
    global $post;
    extract( shortcode_atts( array(
    'posttype'      => '',
    'meta_key'      => '',
    'priority'      => '',
    'meta_compare'  => '',
    'layout'        => 'rows',
    'cols'          => 1, 
    'tag'           => '',
    'count'         => 10, 
    'orderby'       => 'date',
    'order'         => 'DESC'
    ), $params ) );

    //Set $posttpe array
    $posttype = explode( ',', $posttype );

    $args = array(
        'post_type' => $posttype,
    );
    $myquery = new WP_Query( $args );  
    ob_start();
    ?>
    <div class="row">
    <?php
        // The Loop
        if( $myquery->have_posts() ) : while( $myquery->have_posts() ) :
        $myquery->the_post();
    ?>

    <?php 
    // check if 'cpt_press' is in the $posttype array
    if( in_array( 'cpt_press', $posttype ) ) : ?>
        <?php the_content(); ?>
    <?php else : ?>
        <?php the_excerpt(); ?>
    <?php endif; ?>

    <?php endwhile; endif; wp_reset_postdata(); ?>
    </div>
    <?php 
    return ob_get_clean(); 
}
add_shortcode('myquery', 'myshortcode');
share|improve this answer

I see two problems here. One is that the explode() doesn't actually specify the correct split string, which is ', ', WITH a space.

What is written in the sample:

'post_type' => explode( ',', $posttype ),

What is written in the example shortcode:

[myquery posttype='cpt_press, cpt_two, cpt_three, cpt_etc']

Note that there is a comma AND a space between each post type. I would use preg_split() instead to allow for some flexibility in the way this value could be written in the shortcode:

'post_type' => preg_split( '/\s*,\s*/', $posttype ),

This will allow any amount of white-space to be used on either side of each comma between the post types in the given list string.

The second problem I see is that the given code is testing the $posttype variable for equality to ''cpt_press'`, even though it could be a comma-separated list.

Instead, check for membership of 'cpt_press' in the $args['post_type'], array, which will set to the list of the post types given in the posttype shortcode argument:

in_array( 'cpt_press', $args['post_type'] )

The updated code would thus be:

function myshortcode( $params, $content = null ) {
    global $post;
    extract( shortcode_atts( array(
        'posttype'      => '',
        'meta_key'      => '',
        'priority'      => '',
        'meta_compare'  => '',
        'layout'        => 'rows',
        'cols'          => 1, 
        'tag'           => '',
        'count'         => 10, 
        'orderby'       => 'date',
        'order'         => 'DESC'
    ), $params ) );   
    $args = array(
        'post_type' => preg_split( '/\s*,\s*/', $posttype ),
     );    
    $myquery = new WP_Query( $args );  
    ob_start();       
    ?><div class="row"><?php    
    // The Loop
    if ( $myquery->have_posts() ) : while( $myquery->have_posts() ) : $myquery->the_post();    
        if ( in_array( 'cpt_press', $args['post_type'] ) ) :              
            the_content();   
        else :
            the_excerpt();
        endif;
    endwhile; 
    endif;
    wp_reset_postdata();
    ?></div><?php 
    return ob_get_clean(); 
}    
add_shortcode( 'myquery', 'myshortcode' );  
share|improve this answer

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.