• rrusch

    (@rrusch)


    I am using the code below to order my posts by the amount of views it has.

    <?php $args = array(
    'meta_key' => 'views',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    ); ?>
    
    <?php query_posts($args); ?>

    By changing the meta_key to ‘_likes’ I can order posts by the amount of likes it has.

    I want the user to be able to choose between views and likes with a simple drop down menu or buttons.

    I’ve searched and searched for a way to do this but cant find any solutions!

Viewing 10 replies - 1 through 10 (of 10 total)
  • deepbevel

    (@deepbevel)

    posible solution without javascript:
    create a custom page for the likes, and one for the views. Then apply code accordingly. Add the pages to menu. Done!

    deepbevel

    (@deepbevel)

    Or, in your standard page template:

    <?php if(is_page('page-id-here') ) { ?>
    <?php $args = array(
    'meta_key' => 'views',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    ); ?>
    <?php query_posts($args); ?>
    //output
    <?php } ?>
    <?php if(is_page('page-id-here') ) { ?>
    <?php $args = array(
    'meta_key' => 'likes',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    ); ?>
    <?php query_posts($args); ?>
    //output
    <?php } ?>

    Thread Starter rrusch

    (@rrusch)

    I tried this but the theme that I’m using does not have a blog template it just uses the index.php as the blog.

    When I made a template by copying the index.php code and applied it to a page it was all messed up and nothing worked.

    I contacted the theme creators via their support forum and this was the conclusion:

    Unfortunately, you cannot change the template applied to the index.php file on the fly. I think you’ll need to create additional page templates with custom queries for each and custom css as well. Step-by-step instructions on making this all happen is beyond the scope of this forum, I’m afraid.

    Now I’m here trying to find a more elegant and simple solution!

    deepbevel

    (@deepbevel)

    yes:

    create a custom page for the likes, and one for the views.

    that’s what I suspected, wheras putting it in the standard template was only a shot.

    You should try it, put that code in a custom template and add some output, then see how it might be styled. You can do it!

    don’t copy index.php, copy your page template but remove the loop if it has one,then supply your own.

    deepbevel

    (@deepbevel)

    if you aren’t using a child theme, back up your original template customizations (if any) and have access to the original theme files.

    Thread Starter rrusch

    (@rrusch)

    OK I’m currently working through the css to repair the broken parts and so far it seems to be coming back to life.

    It may take a while though. I guess I was just being lazy and wanted a quick fix!

    deepbevel

    (@deepbevel)

    oh well, it’s more often more work than we expect, carry on, and good luck.

    Rajesh Soni

    (@rajeshsoni)

    You could add a dropdown / radio button on the search form and use this modified code (assuming you name this new field show_likes):

    $default_view = 'views';
    if($_REQUEST['show_likes'])
    $default_view = '_likes';
    
    $args = array(
    'meta_key' => $default_view,
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    );
    
    query_posts($args);
    Thread Starter rrusch

    (@rrusch)

    Wow, works like a dream, thank you so much!

    You have saved me a lot of work.

    Rajesh Soni

    (@rajeshsoni)

    Sounds great!

    Glad I could be of help.

    Cheers!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Let user order posts by meta key’ is closed to new replies.