Support Forum » User Profile

Forum Replies Created

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • in reply to: Filter Archive View by Custom Field #20908
    AnsonAnson
    Participant

    Solved the problem with this!

    <?php
    
    global $wpdb;
    $ids = $wpdb->get_col( "SELECT ID FROM cities WHERE countryisocode = 'cn'" );
    
    if ( empty( $ids ) ) {
        echo 'There is no posts';
    } else {
        $query = new WP_Query( [
            'post_type' => 'city',
            'post__in'  => $ids,
        ] );
      
          while ( $query->have_posts() ) {
            $query->the_post();
            echo get_the_title() . '<br>';
            
          }
        // Do something
    }
    
    ?>
    in reply to: Filter Archive View by Custom Field #20903
    AnsonAnson
    Participant

    Hi Long,

    I have gone through the links and also took reference from the link below.

    https://support.metabox.io/topic/meta_query-with-mb-custom-tables/

    I am now trying to filter 'Cities' by country code ('us') obtain from the custom table.

    Can you advise what's wrong with the following code? I am getting the result 'nothing here'.

    <?php
    global $wpdb;
    $post_ids = $wpdb->get_col( $wpdb->prepare(
        "SELECT ID FROM cities
        WHERE countryisocode = 'us'",
        $post->post_author
    ) );
    
    $query_args = array (
        'post_type' => 'city',
        'orderby'   => 'meta_value',
        'meta_key'  => 'countryisocode',
        'order'     => 'ASC',
        'post__in'  => $post_ids,
    );
    
    //The query
    $cities_in_country = new WP_Query( $query_args );
    
    // The Loop
    if ( $cities_in_country->have_posts() ) {
        echo '<ul>';
        while ( $cities_in_country->have_posts() ) {
            $cities_in_country->the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
        echo '</ul>';
    } else {
      
     echo "nothing here";
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    ?>

    Thank you.

    in reply to: Filter Archive View by Custom Field #20886
    AnsonAnson
    Participant

    Hi Long,

    I have shared the full code in MB View here : https://pastebin.com/qPHbPTPG

    in reply to: Filter Archive View by Custom Field #20875
    AnsonAnson
    Participant

    I uploaded an example here : https://imgur.com/a/aEsXAXj

    in reply to: Filter Archive View by Custom Field #20874
    AnsonAnson
    Participant

    Hi Long,

    Thank you so much for your help.

    Your Loom video is very clear and helpful. I just checked with a few more random country codes and found that it is working.

    However, strangely the posts that matches the countryisocode are spread across different pages (with most pages blank). I have pagination enabled and it shows 30 pages even though the actual number of 'VN' records is only 2. The custom table has 300 rows of data.

    The records that matches 'VN' for 'countryisocode' can be found in these urls:

    /archive-slug/page/1
    /archive-slug/page/25/

    in reply to: Filter Archive View by Custom Field #20864
    AnsonAnson
    Participant

    Hi Long,

    I am getting a blank result with the filter and I tried to filter using other custom fields as well.

    I am using MB Custom Table..

Viewing 6 posts - 1 through 6 (of 6 total)