Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Set featured posts in tags and categories, and higher than other featured posts

  • HI. I would like to know if there is a wordpress code that allows me to set a featured article that is also high in tags and categories, and that it always remains higher than other articles marked as featured.

    Is there a code to achieve this in a simple way, and without using plugins?

    Thank you,
    Daniele

  • Hi Daniel,

    Intrinsically, there’s no feature in GP to accomplish such.

    What you can do is create a Block Element – Loop Template and manually create the Loop with your featured image.

    Without a plugin, it would be best to reach out to WordPress forums regarding this as this would be out of our scope.

    With a plugin, you could probably try this to allow Sticky Posts on Categories: https://www.wpbeginner.com/plugins/how-to-add-sticky-posts-for-categories-in-wordpress/

    More info on Sticky Posts: https://developer.wordpress.org/themes/functionality/sticky-posts/

  • Hello Fernando thanks 1000!!! I also tried with the plugin that you proposed. Ok for the categories and tags, but I can’t solve a problem which is the following: For example, after having marked 5 articles as featured, I would also like to have the possibility to choose one of these 5 articles, which would be positioned higher of all the others.

    This doesn’t seem possible, yet it often happens that you want to make your best articles appear higher than the others, and among these one in particular that goes even higher 🙂

    Daniele

  • Hi there,

    that would require custom development.
    You can use the pre_get_posts filter to order posts by a post meta value ( custom field ),
    See here for an example:

    https://generatepress.com/forums/topic/cpt-archive-page-all-posts-order-by-cpts-category-taxonomy/#post-2165739

  • Thanks David. Is this code right?

    ////////////////////////////////////////////////////

    add_filter( ‘generate_elements_custom_args’, function( $args ) {
    $args[‘suppress_filters’] = true;
    return $args;

    } );

    function custom_cpt_order($query){
    if( !is_admin() && ‘reference’ == get_post_type() && !is_single() && empty( $query->query_vars[‘suppress_filters’] ){
    $query->query_vars[‘meta_key’] = ‘ref-rok’;
    $query->query_vars[‘orderby’] = ‘meta_value’;
    $query->query_vars[‘order’] = ‘DESC’;
    }
    return $query;
    }

    add_filter( ‘pre_get_posts’, ‘custom_cpt_order’ );

    ///////////////////////////////////////////////////////////

    Do I have to put it in the functions.php file using code snippet?

    Do I have to make any changes before inserting it?

    Thanks,
    Daniele

  • You need to replace ref-rok with your meta key.

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

  • HI Fernando! What exactly do you mean by “meta key”?

    In private I sent you the url of the article that within 10 articles marked as highlighted, I would like it to stay higher than all 🙂

    Thank you 1000 for your kindness!

    Daniele

  • You need to create custom Post Meta field for your Posts through a plugin like ACF: https://www.advancedcustomfields.com/resources/getting-started-with-acf/

    The meta you set for your field is the meta key we’re referring to.

    This meta is what would be used to order your posts.

  • Hi Fernando! I put the code you gave me, but it warns me of a syntax error in line 8. Can you see it?

    https://i.postimg.cc/SKj226LD/error.jpg

    //////////////////

    add_filter( ‘generate_elements_custom_args’, function( $args ) {
    $args[‘suppress_filters’] = true;
    return $args;

    } );

    function custom_cpt_order($query){
    if( !is_admin() && ‘reference’ == get_post_type() && !is_single() && empty( $query->query_vars[‘suppress_filters’] ){
    $query->query_vars[‘meta_key’] = ‘ref-rok’;
    $query->query_vars[‘orderby’] = ‘meta_value’;
    $query->query_vars[‘order’] = ‘DESC’;
    }
    return $query;
    }

    add_filter( ‘pre_get_posts’, ‘custom_cpt_order’ );

    //////////////////////////////////////////////

    Thanks,
    Daniele

  • Replace all the curly quotation marks with straight quotation marks.

  • Sorry Fernando, the translation is not perfect and I don’t understand what exactly you mean, could you make this edit for me please?

  • Try this instead:

    add_filter( 'generate_elements_custom_args', function( $args ) {
    $args['suppress_filters'] = true;
    return $args;
    
    } );
    
    function custom_cpt_order($query){
    if( !is_admin() && 'reference' == get_post_type() && !is_single() && empty( $query->query_vars['suppress_filters']) ){
    $query->query_vars['meta_key'] = 'ref-rok';
    $query->query_vars['orderby'] = 'meta_value';
    $query->query_vars['order'] = 'DESC';
    }
    return $query;
    }

    I corrected the syntax error.

  • Hi Fernando! Many thanks, now the script has no more errors! However, after doing all the work, no posts are displayed any higher. Nothing happened at all! What am I doing wrong?

    Below, I show you what I did on a test site:

    https://i.postimg.cc/gJ5snV3f/01.jpg
    https://i.postimg.cc/8c6HW7Hs/02.jpg
    https://i.postimg.cc/dQkngfYv/03.jpg
    https://i.postimg.cc/nVyTx1Fm/04.jpg

    Thank you, you’re so kind!

  • Use the “Field Name” not that Key as the meta_key.

  • I tried as you said, but still nothing appears 🙁 Maybe I’ll never succeed >_<

    https://postimg.cc/NKj9xnvp

    Thanks…

  • It might also be good to reach out to a developer regarding this as this would be out of our scope.

    But, the code provided should work. There may be something you have missed or done incorrectly. Kindly check this entire thread again: https://generatepress.com/forums/topic/cpt-archive-page-all-posts-order-by-cpts-category-taxonomy/

Viewing 16 posts - 1 through 16 (of 16 total)
  • You must be logged in to reply to this topic.