Ready to get started?Download WordPress


belongs_to_dropdown does not populate (3 posts)

  1. kevcpu
    Posted 2 years ago #

    I am trying to create a foreign key relationship between one of my tables and the wp_posts table by putting the post_id in my table. What do I have to do to populate the dropdown with the posts? So far the belongs_to_dropdown is showing up empty.


  2. dodmax
    Posted 2 years ago #

    AS far as I know you have to provide the list yourself to the belongs_to_dropdown() function.

    The way to do it is to override the add and the edit functions in your admin controller and to query it there then use set_object to make it available in the view (where the belongs_to_dropdown call is).

    Have a look at the event-calendar-example for an example:

  3. kevcpu
    Posted 2 years ago #

    I am trying this and to shed some additional light, here is my set_post method:

    private function set_post() {
    		$posts = $this->Post->find(array('selects' => array('id', 'post_title')));
    		$this->set('posts', $posts);

    In the dropdown I want to use the id field and the post_title as the text. Also, here is the call I am making to the belongs_to_dropdown:

    echo $this->form->belongs_to_dropdown('Post', $posts, array('style' => 'width: 200px;', 'empty' => true, 'id' => 'drpPost', 'label' => 'Post connected to entry'));

    The dropdown populates and the text field is empty, but the id field is populated.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic


No tags yet.