• Hi, guys, I have this problem and hope that someone can give me a solution. I use it in my WordPress locations like USA, California, and Los Angeles. Now I need to do the following using custom PHP functions:

    1) Get from the WordPress the post ID corresponding to each country, state, and city, so in the example USA, California, and Los Angeles and then render the result of the WordPress ID as follows:

    _11_,_22_,_33_
    where 11 is for the USA, 22 is for California and 33 is for Los Angeles.

    How I can do that? Many thanks for your help

    Alfred

    • This topic was modified 2 years, 8 months ago by Jan Dembowski.
Viewing 3 replies - 1 through 3 (of 3 total)
  • If they are post IDs then it’s not hard query. The only thing that you need is a way to know which ones are “state”s and which are not.

    The easy way to do that is using a category called ‘State’, and you can query off that. As an example…

    $states = get_posts (array (
        'post_type' => 'post'.
        'numberposts' => -1,
        'orderby' => 'ID',
        'order' => 'ASC',
        'category' => 1 // Change for the 'state' category ID
    ));
    
    foreach ($states as $state) {
        echo $state->ID;
    }

    Of course, if you categorise the posts in a different way, what you want might easily be different from this.

    Thread Starter alfred2022

    (@alfred2022)

    Hi, catacaustic, and thanks for your reply and help. So with this code, I can get the ID of the State. How I can build the code to render the string composition? Thanks

    $sep = '';
    
    foreach ($states as $state) {
        echo $sep. '__' . $state->ID . '__';
        $sep = ',';
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to code this with Php functions’ is closed to new replies.