0

I can't get the titles of my custom post.

this is what i'm doing:

//IN functions.php

function fluid_scripts() {
    wp_enqueue_script('lateral-fluid', get_template_directory_uri().'/js/lateral-fluid.js', array('jquery'));
    wp_localize_script( 'lateral-fluid', 'ajaxFluid', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
}

add_action('init', 'create_post_type');
function create_post_type(){
    resgister_post_type ( 'portfolio',
            array (
                'labels' => array(
                'name' => __('beds'),
                'singular_name' => __('bed')
            ),
            'public' => true,
            'has_archive' => true,
            )
    );
}

add_action( 'wp_ajax_nopriv_lateral_fluid', 'my_lateral_fluid' );
add_action( 'wp_ajax_lateral_fluid', 'my_lateral_fluid' );

function my_lateral_fluid() {
    //echo "test";
    $args = array(   
            "post_type" => "portfolio",
            "posts_per_page" => -1  
    );  
    $portfolio_query = new WP_Query($args);                 

    while($portfolio_query->have_post() ){
        $portfolio_query->the_post();
        echo "<li>" . get_the_title() . "</li>";       
    }
    wp_die();
}

I check in my admin-ajax.php url with my action: http://localhost/wordpress/wp-admin/admin-ajax.php?action=lateral_fluid, and it didn't display anything. If I uncomment //echo "test"; it display test. I already add 5 custom-post with their titles.

Why I don't get the titles of my custom-post (portfolio). What I'am missing?

Thanks in advanced

1 Answer 1

2

First replace:

resgister_post_type 

with:

register_post_type 

to fix the typo.

Secondly replace:

while( $portfolio_query->have_post() ){

with

while( $portfolio_query->have_posts() ){

where you've forgotten the plural s.

Note

The reason why calling non-existing methods like:

$portfolio_query->some_non_existent_method()

doesn't raise a PHP error, is the magic __call method of the WP_Query class:

/**
 * Make private/protected methods readable for backwards compatibility.
 *
 * @since 4.0.0
 * @access public
 *
 * @param callable $name      Method to call.
 * @param array    $arguments Arguments to pass when calling.
 * @return mixed|false Return value of the callback, false otherwise.
 */
public function __call( $name, $arguments ) {
    if ( in_array( $name, $this->compat_methods ) ) {
            return call_user_func_array( array( $this, $name ), $arguments );
    }
    return false;
}

Without it we would get the expected:

Fatal error: Call to undefined method WP_Query::some_non_existent_method()

So making typos in WP_Query method names can be tricky to detect.

I think it might be better to throw an exception instead of returning false for non-compatible methods:

throw new Exception(
    sprintf(
        __( 'Call to undefined method WP_Query::%s' ),
        sanitize_key( $name )
    )
);
7
  • I feel really stupid !!!! Thank you very much, I was starting to go crazy!!! Commented Dec 7, 2015 at 3:40
  • Don't be, these kind of typos in the method name can be sneaky because they don't raise PHP errors ;-) Good IDE might help @PabloLevin
    – birgire
    Commented Dec 7, 2015 at 3:42
  • Yes and I'm new in wordpress. Could you orient me in: How could I get the id and the permalink to use with js? So I can load a custom-post into a <div> Commented Dec 7, 2015 at 5:07
  • Check out the wp_localize_script() function @PabloLevin
    – birgire
    Commented Dec 7, 2015 at 8:18
  • 1
    Hahahaha yes. It is strange to me that something has never come up like the exception error as we do get posts here from time to time where have_posts() and the_post() have been spelled wrong. A proper exception error would have saved those poor souls their mental health. Good idea to submit this for future core changes ;-) Commented Dec 7, 2015 at 8:38

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.