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 have a shortcode where I'm trying to get a specific string from the $args array below.

Query:

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

Conditional:

<?php if( $posttype == 'cpt_press' ) : ?>
  <?php the_content(); ?>
<?php else : ?>
  <?php the_excerpt(); ?>
<?php 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? Thanks so much!

UPDATED CODE BLOCK

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

$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();
?>

<?php if( $posttype == 'cpt_press' ) : ?>
  <?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 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 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 at 17:36
    
@1fixdotio Thanks I've already tried in_array :( –  Tai Sem Mar 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 at 17:46
    
Have you var_dump the "$posttype" and make sure it is an array with "cpt_press" in it? –  1fixdotio Mar 14 at 17:46

1 Answer 1

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

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.