Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End How to 'query' a Custom Post Type

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #157571
    Ailsa_C
    Participant

    Hi,
    I’m trying to create a shortcode which will pull content from a custom post type into a page. The below works perfect if I want to pull a page into a page but not my custom post type into a page. I guess i have to ‘query’ my custom post type which is called ‘speakers’? but I don’t know how to alter the code to do that (as i’m useless at php…can merely copy and paste!)
    Many thanks.

    ` // extract atts and assign to array
    extract(shortcode_atts(array(
    “page” => ” // default value could be placed here
    ), $atts));

    // if a page id is specified, then run query
    if (!empty($page)) {
    $pageContent = new WP_query();
    $pageContent->query(array(‘page_id’ => $page));
    while ($pageContent->have_posts()) : $pageContent->the_post();
    // assign the content to $output
    $output = get_the_content();
    endwhile;
    }

    return $output;
    }
    add_shortcode(‘pa_insert’, ‘pa_insertPage’);`

    #157578
    Senff
    Participant

    A very simple query for a custom post type would be something like this:

    $custquery = new WP_query (
        array(
        'post_type'=> 'speakers',
        'posts_per_page' => 5
        )
    );
    
    while ( $custquery -> have_posts() ) : $custquery -> the_post();
    
        echo the_title();
        echo the_content();
    
    endwhile;   
    
    wp_reset_query();               
    
    #157595
    Ailsa_C
    Participant

    Hi Senff,
    I put that code in but it doesn’t make any difference -although I’m so bad at php I wouldn’t even know where to put it – I just pasted it in before:
    // if a page id is specified, then run query

    Maybe I should have removed something too?

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.