0

This code is going into a sample Plugin I am creating as a test using CPT and print them to a Page.

I have been trying to get print_r() to give me an object. The custom post types are posting - verified. The data is being stored in MySQL - verified. Thus the post type is registering.

Please provide me with a new set of eyes to tell me what I am missing in my query.

add_action('init', 'all_custom_post_types');

function all_custom_post_types() {

  $types = array(
    // Pledge Items
    array('the_type' => 'testimonial',
    'single' => 'Testimonial',
    'plural' => 'Testimonials'));

  foreach ($types as $type) {

  $the_type = $type['the_type'];
  $single = $type['single'];
  $plural = $type['plural'];

  $labels = array(
  'name' => _x($plural, 'post type general name'),
  'singular_name' => _x($single, 'post type singular name'),
  'add_new' => _x('Add New', $single),
  'add_new_item' => __('Add New '. $single),
  'edit_item' => __('Edit '.$single),
  'new_item' => __('New '.$single),
  'view_item' => __('View '.$single),
  'search_items' => __('Search '.$plural),
  'not_found' => __('No '.$plural.' found'),
  'not_found_in_trash' => __('No '.$plural.' found in Trash'),
  'parent_item_colon' => ''
  );

  $args = array(
  'labels'             => $labels,
  'public'             => true,
  'has_archive'        => true,
  'publicly_queryable' => true,
  'show_ui'            => true,
  'query_var'          => true,
  'rewrite'            => true,
  'capability_type'    => 'post',
  'hierarchical'       => false,
  'menu_position'      => 5,
  'supports'           => array('title','editor','thumbnail','custom-fields','excerpt'));

register_post_type($the_type, $args);

  }

}

////////////////////////

function testimonials_list() {
    if (is_page('9595')) {

    $the_query = new WP_Query( array(
        'post_type' => 'Testimonials'
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    print_r($the_query);
endwhile;

    }
}

add_action('pre_get_posts','testimonials_list');

2 Answers 2

1

Try:

    $the_query = new WP_Query( array(
      'post_type' => 'Testimonials'
     ) );

Looks like you probably just need to change that to :

$the_query = new WP_Query(
    array( 'post_type' => 'testimonial' )
);

You need to use the name you created it with when looking at post_type

1
  • Check your bracket count after making the edit. Commented Jan 27, 2015 at 23:55
0

Your issue currently is that pre_get_posts fires for every query within a request. You're creating an infinite loop which terminates with a fatal out of memory error. If you enable debugging, you should see the error message.

Your testimonials_list function fires for the first query on your page, and the condition is_page('9595') is met, and you create a new query, $the_query. This in turn fires the same testimonials_list function, which also meets the is_page('9595') conditional, and creates a new query. This happens over and over until PHP runs out of memory.

The answer involves an additional check, like is_main_query(), and/or remove_action to remove the action after it runs once.

2
  • Thanks for the tip on this. I think I am getting closer: add_action( 'pre_get_posts', 'cv_testimonials_list'); function cv_testimonials_list($query) { if ($query->is_page('9595') && $query->is_main_query()) { gravity_form(1, false, false, false, '', false); $args = array ( 'post_type' => 'testimonial', 'post_status' => 'published', 'pagination' => true, 'posts_per_page' => '10', 'order' => 'DESC', 'orderby' => 'date', ); // The Query $query = new WP_Query( $args ); print_r($query); return; } }
    – OldWest
    Commented Jan 28, 2015 at 2:32
  • The problem I am facing now is the header is populating with the content of the query and not the_content area of the Page.
    – OldWest
    Commented Jan 28, 2015 at 2:32

Your Answer

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

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