0

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' );    
9
  • 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. Commented Mar 14, 2014 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 ) ). Commented Mar 14, 2014 at 17:36
  • @1fixdotio Thanks I've already tried in_array :( Commented Mar 14, 2014 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. Commented Mar 14, 2014 at 17:46
  • Have you var_dump the "$posttype" and make sure it is an array with "cpt_press" in it? Commented Mar 14, 2014 at 17:46

1 Answer 1

1

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' );  

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.