-1
I intend to use the code below to sort and group by posts section of my Wordpress site. This would be applied to Home Page and Categories, where each post would feature the css class according to its section (secao_1, secao_2 or secao_3), with the parent div of these elements being id #content. The question I ask is: to what extent could this harm the performance of the site in its upload? Also, would it be ideal to add the code inside the HEAD tag, so that the loading is (from the visual point of view of the elements) faster? Another detail, each page would have 30 posts.
var array = ['secao_1', 'secao_2', 'secao_3'];
$.each(array,function(index,value){
$('#content').append($('.'+value));
});
After testing with Jquery, I’m trying to get to what I want through meta_key and meta_value of fields created with the ACF plugin, where I’m using the Loops below. The 1st and 2nd loop search for meta_values secao_1 and secao_2, respectively. The 3rd loop displays posts that do not have any section.
My question is whether this code can be improved. In addition, H2 tags end up appearing on page 2 of the category, even if there are no posts from their respective sections, how to avoid this?
<?php
global $query_string; parse_str( $query_string, $my_query_array );
$paged = ( isset( $my_query_array['paged'] ) && !empty( $my_query_array['paged'] ) ) ? $my_query_array['paged'] : 1;
$multi_type = array('post','post2');
// The Query
$query1 = new WP_Query(array(
'paged' => $paged,
'post_type' => $multi_type,
'meta_key' => 'sec_especial',
'meta_value' => 'secao_1',
'category_name' => 'featured'
));
// The Loop
echo '<h2>Seção 1</h2>';
while ( $query1->have_posts() ) {
$query1->the_post();
get_template_part( 'content', get_post_format() );
}
// Restore original Post Data
wp_reset_postdata();
/* The 2nd Query (without global var) */
$query2 = new WP_Query(array(
'paged' => $paged,
'post_type' => $multi_type,
'meta_key' => 'sec_especial',
'meta_value' => 'secao_2',
'category_name' => 'featured'
));
// The 2nd Loop
echo '<h2>Seção 2</h2>';
while ( $query2->have_posts() ) {
$query2->the_post();
get_template_part( 'content', get_post_format() );
}
// Restore original Post Data
wp_reset_postdata();
/* The 3nd Query (without global var) */
$query3 = new WP_Query(array(
'paged' => $paged,
'post_type' => $multi_type,
'category_name'=> 'featured',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'sec_especial',
'value' => false,
'type' => 'BOOLEAN'
),
array(
'key' => 'sec_especial',
'compare' => 'NOT EXISTS'
)
)
));
// The 3nd Loop
echo '<h2>Seção 3</h2>';
while ( $query3->have_posts() ) {
$query3->the_post();
get_template_part( 'content', get_post_format() );
}
// Restore original Post Data
wp_reset_postdata();
twentyfourteen_paging_nav();
?>
Thank you for your answer, Caio. I have been testing with Jquery and searching for alternatives with meta_key and meta_value, creating custom fields with the ACF plugin. In these fields, there would be sections, so when I would create a post I would choose your section. On the category page of this post, I would do a query by meta_key and meta_Value and group everything. I will edit the question and add the code.
– Rafael
@Rafael your edition has totally changed the content of the question. I suggest you reverse it and, if you feel it necessary, ask another question.
– Caio Felipe Pereira