Wordpress Exclude Categories Posts but retain Post Count per page - wordpress

So I am excluding posts is several categories from a blog page (categories 4-11).
I am doing so using the following code:
if (have_posts()) : while (have_posts()) : the_post();
$category = get_the_category();
if($category[0]->cat_ID > 11 || $category[0]->cat_ID < 4){
continue;
}
This works to exclude the categories posts from the page but it does not retain the post count per page being 10 or whatever it is set to in the Admin.
How would I programatically decrement the post count by one for posts I skip in the Wordpress Loop so I exclude the category posts i do not want but also retain the same amount of posts per page?

The easiest (but not most efficient) way to do this is to replace the global $wp_query by a custom query using category__in...
global $wp_query;
$wp_query = new WP_Query(array("category__in"=>array(4,5,6,7,8,9,10,11)));
...then do the loop...
while (have_posts()){
the_post();
//etc..
}
This will make the paging accurate, and you can rely on the high-level templating functions like next_posts_link().
Probably a more efficient way (that doesn't throw out and replace $wp_query) would be to mess with the original query before it's executed...
add_action('parse_query', 'my_parse_query');
function my_parse_query(&$q){
//decide if you want to mess with the query....
//if not, return
$q->set_query_var("category__in", array(4,5,6,7,8,9,10,11));
}
my_parse_query would be called on every query, including those for pages and single posts, so you would have to add some logic to the function to only add category__in where it made sense.

Related

Make Custom Post Type with custom field inherit category's custom field when empty

been struggling finding a solution to my problem for weeks.
Case :
I have a custom post type named : design. This CPT have a custom field (made with ACF plugin) called thematique. I created the same custom field (thematique) for design's categories.
Expected behaviour:
I want that whenever if we make a get_posts() or WP_Query if a design's thematique field is empty, it should inherit its categorie's thematique.
I've investigated into the pre_get_posts hook but I'm not quite sure how to handle it.
Anybody has an idea ?
Thanks in advance, I really appreciate your help !
You can just do this the easy way and inside your WP Query where you have the formatting for each returned item add this:
<?php $thematique = get_field('thematique'); //Gets current posts value of fiels
<?php if (empty($thematique)){ //Checks if the field is empty, if so do the following
$postCat = get_the_category(); //Get current posts category ID
$catID = 'category_' . $postCat; //Merge category ID and needed string for ACF
$thematique = get_field('thematique', $catID); //Updated the value of $thematique with categories value
}?>
Although not tested this should indeed work as it's how ACF says to get the value from categories. Find out more here.
#Ali_k
I'm not so sure about how to go about it though. I would need something like :
// Designs Thematique priority mechanic
function design_thematique_priority($query){
if($query->query['post_type'] == "design"){
foreach($query->posts as $post){
if($post->thematique == ""){
$post->thematique = $post->category->thematique;
}
}
}
}
add_filter( 'pre_get_posts', 'design_thematique_priority' );
But I don't think there is any loop I can use to loop through posts in pre_get_posts right ?

Wordpress + ACF, display random post images from each post on home page

I’m new to Advanced Custom Fields and a novice in WP, I'm interested in creating a random slider in my homepage where each post has many images.
I’m not 100% sure how to combine the wp_query with the ACF repeater, where multiple posts are involved, I did succeed in doing this in a single post page.
I am less interested in specs on how to do this, nor PHP functions, i’m well versed in both, the issue is the WP functions and conventions
If someone already done something like this and can advise how to begin this with combining the ACF repeater functions together with the wp_query, from there i'd know how to shuffle the images of each post with array_rand.
if a Gist/fiddle exists, would be even better.
You could use the shuffle PHP function to randomise the array the repeater field outputs then slice out the amount of slides that you want. Something like this:
$rows = get_field('repeater_field_name'); // Get row array
shuffle($rows); // Shuffle the array in a random order
$rows = array_slice($rows, 0, 5); // Slice out the first 5 elements of the array
if($rows)
{
echo '<ul>';
foreach($rows as $row)
{
echo '<li>sub_field_1 = ' . $row['sub_field_1'] . ', sub_field_2 = ' . $row['sub_field_2'] .', etc</li>';
}
echo '</ul>';
}
If you're just using the repeaters for images, I would suggest using the "Gallery" element rather than the repeater, and doing the same thing.

I see my URL but I can't find the page in wordpress dashboard

I'm using wordpress and my page has the URL http://proservicescontractors.com/services/
But when I go to the page in my dashboard with the above URL, any change I make does not show on the front end. I tried simply duplicating my content and that change did not show on the front end.
Not sure what to do, this has me completely baffled.
Any ideas?
Since they're custom post types, by default, they're not actually loaded into a page per se. You should read up on WordPress's template hierarchy. To give you a rough idea of what's happening:
WP looks at your URL, and since it recognises it as a custom post type archive, it will look for a template to use...
It will first look for archive-$post_type.php, or in your case, archive-services.php
If it can't find that, it will look for archive.php
If it can't find that, it will use index.php
The important thing to note is that archive pages don't actually show up in the admin area, since they simply gather up and display custom posts, so there's nothing for you to edit.
Now, if you really want to edit some content on the Services archive, you have two options:
Edit archive-services.php in a text editor.
This is the quick and dirty option; the downside is that it defies the point of a CMS.
Create a page template with it's own loop
Create a new page template called page-services.php and insert a loop in there to display your custom posts. To get you started:
<?php get_header(); ?>
<?php // The main loop
if (have_posts()) {
while (have_posts()) {
the_post();
}
} else {
echo 'No posts';
}
?>
<?php // Now for the services loop
// WP_Query arguments
// For additional options, see: https://codex.wordpress.org/Class_Reference/WP_Query#Parameters
$args = array (
'post_type' => array( 'services' ),
);
// The Query itself
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Do something with the post
// In your case look at archive-services.php and see what
// that template does inside the loop
}
} else {
// no posts found
}
// Restore original Post Data
// Don't forget this, it's important
wp_reset_postdata();
?>
<?php get_footer();?>
You should then be able to apply that page template to your Services page; it should then display your posts below the page content. One thing to look out for is that WordPress will continue to load archive-services.php whenever you go to http://proservicescontractors.com/services/. While there are ways around this, the easiest fix would be to simply give your new page a different url, such as http://proservicescontractors.com/all-services/
Thanks for your help. I'm using yoast and I wanted to change the title and description. When you pointed out that it was a custom post type archive and not a page, I went back through yoast and found where I could change them under "Titles and Metas" > "Custom Post Type Archives" > "Services"

make wordpress posts change content dynamically

Is there a way to customise a wordpress blog post so that it changes slightly based on the User?
eg. when showing a post around Salary and Bonuses on the company blog, I'd like to customise some of the text based on the employee level (director, Executive etc) - for example:
a paragraph of text about pension contribution should only appear to
directors reading the page
the bonus amount should change based on employee level.
I don't want to create several pages per category (eg. one page for the directors, one for the execs etc) - I would like to have just one blog post with some variable fields?
The codex has a great function outlined, current_user_can
http://codex.wordpress.org/Function_Reference/current_user_can
As well as WP_User_Query
http://codex.wordpress.org/Class_Reference/WP_User_Query
You can use both of these to accomplish your goal.
I needed to do something similar, I came up with custom roles based on the plugin "Capability Manager Enhanced".
In a file you can check the roles like:
<? if ( is_user_logged_in() ) : ?>
<?
global $current_user;
get_currentuserinfo();
$roles = $current_user->roles; //$roles is an array
if ( is_user_logged_in() && in_array("custom_role", $roles) ) {
echo('WORK HARDER!');
} else {
echo('NO CAKE FOR YOU!');
}
?>
<?php endif; ?>

How to make a plugin to count visitors for posts under specific category

How to count number of visitors for post under specific category ? Can I make such plugin who can do the whole magic ? I don't want plugin users to modify theme files or add code snippets in other theme files ...
something along the lines of adding to the post meta could do what you're wanting.
<?php
add_action('the_content', 'myplugincb');
function myplugincb() {
global $wp_query;
if (count($wp_query->posts) == 1) { //just do this for individual posts/pages
$pid = $wp_query->posts[0]->ID;
$key = 'myplugin_post_visit_counter';
update_post_meta($pid, $key, get_post_meta($pid, $key)+1);
}
}
function myplugin_show_viewed($post_id) {
return get_post_meta($post_id, 'myplugin_post_visit_counter');
}
You'd have to change that quite a few different ways depending on your desired result. You probably want to use something like Google Analytics if you're wanting to see specifics on pages visited and where the user came from etc.

Resources