-
Daniele1974
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 -
Fernando
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/
-
Daniele1974
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 thepre_get_posts
filter to order posts by a post meta value ( custom field ),
See here for an example: -
Daniele1974
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 -
Fernando
You need to replace
ref-rok
with your meta key.Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
-
Daniele1974
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
-
Fernando
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.
-
Daniele1974
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 -
Fernando
Replace all the curly quotation marks with straight quotation marks.
-
Daniele1974
Sorry Fernando, the translation is not perfect and I don’t understand what exactly you mean, could you make this edit for me please?
-
Fernando
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.
-
Daniele1974
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.jpgThank you, you’re so kind!
-
Fernando
Use the “Field Name” not that Key as the meta_key.
-
Daniele1974
I tried as you said, but still nothing appears 🙁 Maybe I’ll never succeed >_<
Thanks…
-
Fernando
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/
- You must be logged in to reply to this topic.