• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

posts sorted with custom fields – pagination problem

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › posts sorted with custom fields – pagination problem

This topic is: not resolved

Tagged: custom field, orderby, pagination, sort

  • This topic has 3 replies, 2 voices, and was last updated 10 years, 5 months ago by cyanoorange.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • February 12, 2016 at 9:43 am #178892
    cyanoorange
    Participant

    Hi,
    I posted a more complex question to this topic before. Now I try to keep it more simple, hoping someone could help me.
    On my page I want my posts be sorted by a custom field "kosmo_sortier_nr":

    add_action('genesis_before_loop','sortierung_Kosmo');
    function sortierung_Kosmo(){
           $aktion = filter_input(INPUT_GET, 'aktion', FILTER_SANITIZE_STRING);
     if($aktion!=2 && is_home()){
        $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
            query_posts('paged=' . $paged . '&posts_per_page=5&meta_key=kosmo_sortier_nr&meta_compare=<=&meta_value_nr&orderby=meta_value&order=ASC'); 
    }
    }

    What happens is: the first 3 pages appear correctly, the following pages show 404.
    Is anybody able to help me with this? What am I doing wrong?
    Would be really great!

    http://kleinekosmonautin.de/wordpress/
    February 12, 2016 at 10:37 am #178902
    Genesis Developer
    Member

    You can try this once

    add_action('genesis_before_loop','sortierung_Kosmo');
    function sortierung_Kosmo(){
      $aktion = filter_input(INPUT_GET, 'aktion', FILTER_SANITIZE_STRING);
      if($aktion!=2 && is_home()){
        $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
        $args = array(
          'paged'           => $paged,
          'posts_per_page'  => 5,
          'meta_key'        => 'kosmo_sortier_nr',
          'meta_compare'    => '<=', 
          'meta_value_nr'   => '', 
          'orderby'         => 'meta_value',
          'order'           => 'ASC'
        );
        
        genesis_custom_loop( $args ); 
      }
    }

    Download Genesis Featured Posts Combo Widget | Simple Grid Layouts Plugin for Posts, CPTs and terms
    You can request new tips/help.

    February 12, 2016 at 10:55 am #178905
    cyanoorange
    Participant

    First of all: thanks for trying to help. I changed the code, but it doesnt work. You can see the result right now on the site.
    Would be great, if you would have another idea.

    February 13, 2016 at 5:51 am #178955
    cyanoorange
    Participant

    I found a solution - the "custom-loop-hint" was important. Thanks for that.
    My solution now is to change the whole loop. But I have the feeling, that this might be not a "best-way-to-do-it" solution. My script now makes the following things possible: Sort post with custom field, sort them via click ASC and DSCN, showing pages.
    Would be great if anybody could give me feedback.
    Here is the important part of the functions.php

    remove_action( 'genesis_loop', 'genesis_do_loop' );
    remove_action( 'genesis_loop', 'prefix_do_loop' );
    add_action('genesis_loop','sortierung_Kosmo');
    function sortierung_Kosmo(){
        /*checkt ob es per Klick auf link eine Aktion gab*/
          $aktion = filter_input(INPUT_GET, 'aktion', FILTER_SANITIZE_STRING);
           /*alte posts zuerst*/
     if($aktion!=2 && is_home()){
        $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
        $args = array(
          'paged'           => $paged,
          'posts_per_page'  => 5,
          'meta_key'        => 'kosmo_sortier_nr',
    //      'meta_compare'    => '<=', 
          'meta_value_nr'   => '', 
          'orderby'         => 'meta_value',
          'order'           => 'ASC'
        );
        
        genesis_custom_loop( $args ); 
     /*neue posts zuerst*/
          }else if($aktion==2 && is_home()){
              $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
        $args = array(
          'paged'           => $paged,
          'posts_per_page'  => 5,
          'meta_key'        => 'kosmo_sortier_nr',
          'meta_value_nr'   => '', 
          'orderby'         => 'meta_value',
          'order'           => 'DSCN'
        );
        
        genesis_custom_loop( $args ); 
          }else{
          if ( is_page_template( 'page_blog.php' ) ) {
    
    		$include = genesis_get_option( 'blog_cat' );
    		$exclude = genesis_get_option( 'blog_cat_exclude' ) ? explode( ',', str_replace( ' ', '', genesis_get_option( 'blog_cat_exclude' ) ) ) : '';
    		$paged   = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    
    		//* Easter Egg
    		$query_args = wp_parse_args(
    			genesis_get_custom_field( 'query_args' ),
    			array(
    				'cat'              => $include,
    				'category__not_in' => $exclude,
    				'showposts'        => genesis_get_option( 'blog_cat_num' ),
    				'paged'            => $paged,
    			)
    		);
    
    		genesis_custom_loop( $query_args );
    	} else {
    		genesis_standard_loop();
    	}
          }
            }
    
  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble