• Resolved jereader

    (@jereader)


    i am trying to populate a select list with all published ad groups and placements (and their associated IDs, assuming they have them) via something like get_posts(). I want the ID as array keys, and the title as values. Is this possible, and how might I go about it?

    for reference, here’s the working code that does what I need but with single ads:

    function get_all_ads(){
    $args = array(
    “numberposts” => -1,
    “orderby” => “post_date”,
    “order” => “DESC”,
    ‘post_type’ => array( ‘advanced_ads’ ),
    “post_status” => “publish”
    );
    $my_posts = get_posts( $args );
    if( ! empty( $my_posts ) ){
    foreach ( $my_posts as $p ){
    $title = $p->post_title;
    $id = $p->ID;
    $output[$id]= $title;
    }
    }
    return $output;
    }

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Thomas Maier

    (@webzunft)

    Hi jereader,

    thanks for reaching out.

    get_posts only allows you to query post types so it would work with our ads but not groups and placements. Groups are taxonomies in WordPress and placements are a plain option in the _options table.

    You can load the ad groups using Advanced_Ads::get_instance()->get_model()->get_ad_groups(). This basically uses get_terms( Advanced_Ads::AD_GROUP_TAXONOMY ).

    You can retrieve the placements array using Advanced_Ads::get_instance()->get_model()->get_ad_placements_array().

    Both functions are used a couple of times in the plugin so you should be able to find examples of their usage there.

    Please let me know if you need anything else.

    Best regards,

    Thomas

    Thread Starter jereader

    (@jereader)

    What would be the equivalent field names for ID and title?

    Thread Starter jereader

    (@jereader)

    Nevermind, I got it figured out.

    Thread Starter jereader

    (@jereader)

    OK, for placements, the function you gave me appears to return the entire array. How would I return the items inside the array individually? Sorry to keep bugging.

    Plugin Author Thomas Maier

    (@webzunft)

    Hi jereader,

    what do you mean by “items inside the array”?

    If you want to display a specific placement then you can use the PHP function with the ID given in the array.

    <?php if(function_exists('the_ad_placement')) the_ad_placement($ID); ?>

    Best regards,
    Thomas

    Thread Starter jereader

    (@jereader)

    I have tried to loop through the array with a foreach similar to the code I shared in my original post, but it doesn’t return any values.

    Plugin Author Thomas Maier

    (@webzunft)

    Is the array empty or what exactly does not return values?

    Thread Starter jereader

    (@jereader)

    the array is not empty. here’s a var_dump of it:

    array(4) {
    [“manual-placement”]=>
    array(3) {
    [“type”]=>
    string(7) “default”
    [“name”]=>
    string(16) “Manual Placement”
    [“item”]=>
    string(8) “ad_15432”
    }
    [“placement-test-1”]=>
    array(3) {
    [“type”]=>
    string(7) “default”
    [“name”]=>
    string(16) “Placement test 1”
    [“item”]=>
    string(10) “group_1374”
    }
    [“header-placement-test”]=>
    array(3) {
    [“type”]=>
    string(6) “header”
    [“name”]=>
    string(21) “Header placement test”
    [“item”]=>
    string(8) “ad_15595”
    }
    [“another-placement-test”]=>
    array(3) {
    [“type”]=>
    string(7) “default”
    [“name”]=>
    string(22) “another placement test”
    [“item”]=>
    string(8) “ad_15595”
    }
    }

    I have added a second, nested foreach loop and am getting values now, but not the ones I need. For instance, how do I get the ID? Here’s my code:

    $my_posts = Advanced_Ads::get_instance()->get_model()->get_ad_placements_array();
    if( ! empty( $my_posts ) ){
    foreach ( $my_posts as $p => $parray){
    foreach($parray as $z => $result){
    $title = $result->name;
    $id = $result->ID;
    $output[$id]= $title;
    }
    }
    }

    I need an array returned with the placement ID as the key and the placement name as the value. But if I do a var_dump of the above code i get:

    array(1) { [“”]=> NULL }

    So, what am I missing or doing wrong in accessing the array that your function returns? I got the ad and ad group working pretty easily.

    Thread Starter jereader

    (@jereader)

    to clarify, I mean a var_dump of $output from the above code.

    Plugin Author Thomas Maier

    (@webzunft)

    Do I understand correctly that the first array is from var_dump( $my_posts )?

    I don’t see where you get the code in the foreach loop from. As you can see in the dump, this is an array, not an object, so $result->ID would not work.

    The placement ID is actually $p in your code and the title of the placement is $parray['name'].

    Thread Starter jereader

    (@jereader)

    Correct, yes, the first var_dump is from $my_posts.

    I made the code up based on my own research. I am not a coder by trade, just figuring this all out on my own – as I’m sure you can tell. That other foreach was because I get confused as shit by arrays. I really appreciate your help with this.

    So, here’s my final code that does exactly what I needed it to:

    <?php
    function get_all_ad_placements(){

    $my_posts = Advanced_Ads::get_instance()->get_model()->get_ad_placements_array();
    if( ! empty( $my_posts ) ){
    foreach ( $my_posts as $p => $parray){
    $name = $parray[‘name’];
    $id = $name;
    $id = strtolower(preg_replace(‘/-+/’, ‘-‘, preg_replace(‘/[^\wáéíóú]/’, ‘-‘, $id)));

    $output[$id] = $name;

    }
    }
    return $output;
    }

    once again, thanks for you help, even though I’m slow and dense!

    Plugin Author Thomas Maier

    (@webzunft)

    Great, I am happy to see that you found a solution.

    Would you find a minute to leave a positive review for Advanced Ads and our support here?

    Thanks,
    Thomas

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘get list of ad groups/placements & ids via php’ is closed to new replies.