• Resolved eian00

    (@eian00)


    Hello, i have this perfectly working code showing posts based on two or more custom fields;

    <?php
    global $wpdb;
    global $post;
    $key1 = 'color';
    $val1 = 'red';
    $key2 = 'shape';
    $val2 = 'square';
    $querystr = "
    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta metacolor, $wpdb->postmeta metawgt
    WHERE wposts.ID = metacolor.post_id
    AND wposts.ID = metawgt.post_id
    AND (metacolor.meta_key = '$key1' AND metacolor.meta_value = '$val1')
    AND (metawgt.meta_key = '$key2' AND metawgt.meta_value = '$val2')
    AND wposts.post_type = 'post'
    AND wposts.post_status = 'publish'
    ORDER BY UPPER(wposts.post_title) ASC
    "; 
    
    $pageposts = $wpdb->get_results($querystr, OBJECT); ?>  <?php if ($pageposts):
    ?>  
    
    <?php global $post; ?>
    <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>
    <div class="post" id="post-<?php the_ID(); ?>">  
    
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">  <?php the_title(); ?></a>
    
    </div>
    <?php endforeach; ?>
    <?php endif; ?>

    now I installed Magic Fields and it looks like it won’t filter posts with custom fields added over Magic fields,

    is there maybe some small editing needed in the code above?

    thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter eian00

    (@eian00)

    I got it,

    the problem was that in magic fields for color I used a Checkbox List with the list of possible colors, so once you choose the color Red for example, wordpress stores it in the database like:

    a:1:{i:0;s:4:"Red";}

    so the correct form would be:

    <?php
    global $wpdb;
    global $post;
    $key1 = 'color';
    $val1 = 'a:1:{i:0;s:4:"Red";}';
    $key2 = 'shape';
    $val2 = 'square';
    $querystr = "
    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta metacolor, $wpdb->postmeta metawgt
    WHERE wposts.ID = metacolor.post_id
    AND wposts.ID = metawgt.post_id
    AND (metacolor.meta_key = '$key1' AND metacolor.meta_value = '$val1')
    AND (metawgt.meta_key = '$key2' AND metawgt.meta_value = '$val2')
    AND wposts.post_type = 'post'
    AND wposts.post_status = 'publish'
    ORDER BY UPPER(wposts.post_title) ASC
    "; 
    
    $pageposts = $wpdb->get_results($querystr, OBJECT); ?>  <?php if ($pageposts):
    ?>  
    
    <?php global $post; ?>
    <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>
    <div class="post" id="post-<?php the_ID(); ?>">  
    
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">  <?php the_title(); ?></a>
    
    </div>
    <?php endforeach; ?>
    <?php endif; ?>

    Hi Eian

    I trying to make query to display list base on one of my magic field checkbox (nz or aus)

    follow is what i have:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Could you help spot me where i did wrong?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using Custom Select Query and Magic fields’ is closed to new replies.