php - Wordpress dynamic array -
i have issue creating array out of post title => post url values (of custom post type).
global $wp_query; $type = 'qa'; $args=array( 'post_type' => $type, 'post_status' => 'publish' ); $array = (); $my_query = new wp_query($args); if( $my_query->have_posts() ) { $title = get_the_title(); $url = get_the_permalink(); $data[] = array('key1'=>$title, 'key2'=>$url); } i tried , couple other combinations without luck.
end result have dynamic array i'm able search ajax in jquery.
that part working static array can't manage dynamic , working.
exact format need is:
$data = array( "post 1 title" => "link post 1", "post 2 title" => "link post 2", "post 3 title" => "link post 3" ); many help!
your loop incomplete. trying build array before starting loop. need move inside loop , build array.
just tip, because using custom post type, rather use get_post_permalink post permalink
$type = 'qa'; $args=array( 'post_type' => $type, 'post_status' => 'publish' ); $my_query = new wp_query($args); $data = array(); if( $my_query->have_posts() ) { while ( $my_query->have_posts() ) { $title = get_the_title(); $url = get_post_permalink(); $data[$title] = $url; } wp_reset_postdata(); } var_dump( $data );
Comments
Post a Comment