Forums

[resolved] Help with a bit of PHP please (3 posts)

  1. colourblindsl
    Member
    Posted 4 months ago #

    Hi all
    New here... hope you nice guys will help me out!

    I'm using this bit of PHP in one of my category templates to display posts sorted by custom field 'prod-catnos' in descending order.

    <?php
    {
         $posts = query_posts($query_string .
    '&meta_key=prod-catnos&orderby=meta_value&order=desc');
    }
    ?>

    .. and it's working fine.

    I've also made a custom field so that the owner of the site can choose to display posts in desc or asc by making a drop box selection, this field is called 'product_order'.
    I can access and print the result of this selection by using:

    <?php  echo get_post_meta('1671', 'product_order', true); ?>

    This also is working fine.

    Now, I'd like the 'order=desc' in the first bit of PHP to be dynamically replaced by the value (asc or desc) of the 2nd bit of PHP, could someone show me how to do this??

    Many thanks in advance
    SL chai

  2. vtxyzzy
    Member
    Posted 4 months ago #

    I added in a test to make sure that $order was either 'asc' or 'desc' and defaulted to 'desc' if not:

    <?php
    {
         $order = get_post_meta('1671', 'product_order', true);
         $order =  (preg_match('/^((asc)|(desc))$/i',$order,$matches)) ? $matches[0] : 'DESC';
         $posts = query_posts($query_string .
    '&meta_key=prod-catnos&orderby=meta_value&order=' . $order);
    }
    ?>
  3. colourblindsl
    Member
    Posted 4 months ago #

    MANY THANKS, vtxyzzy!
    It works like a charm!!
    If you're ever in London, I'll gladly buy you a coffee!!
    All the best
    sl chai

Reply

You must log in to post.

About this Topic

Tags