The right way to close $query->is_home() loop - php

I have tried a lot of ways to close this loop, but my page always crashes or it does not work the right way. I am new to Wordpress and might have some wrong logic here.
The goal is that when I publish a post with category "weekspicture", there will be automatically published copy of the post 1062. It works fine otherwise, just publishes the post 1062 seven times instead of one.
I have tried to google this problem, but do not understand how should I properly close the loop? Or is that the problem anyway?
function poll_after_weekspicture($query) {
if ( $query->is_home() ) {
if ( get_post_type( $post->ID ) == 'post' && in_category(
'weekspicture') ) {
$new_post_id = copyofpoll($post);
// Give the category 14
wp_set_post_categories( $new_post_id, array('14') );
// Here I am trying to close the loop
wp_reset_postdata();
}
}
return $query;
}
And in case it matters, here the function copyofpoll(). The $weekspicture_id does not yet do anything, that is not mistake.
function copyofpoll($weekspicture_id){
$title = get_the_title('1062');
$oldpost = get_post('1062');
$post = array(
'post_title' => 'Automatic poll',
'post_status' => 'publish',
'post_type' => $oldpost->post_type,
'post_author' => 1
);
$new_post_id = wp_insert_post($post);
// Copy post metadata
$data = get_post_custom('1062');
foreach ( $data as $key => $values) {
foreach ($values as $value) {
add_post_meta( $new_post_id, $key, $value );
}
}
return $new_post_id;
}

Related

Exclude specific category from Related Posts

Looking to exclude specific category (ID = 100) from "Related Posts" feed at bottom of Blog pages. Extra bonus if it can also exclude from the sidebar archive (not sure if they are connected??)
I'm using WP Theme "TheFox", have asked them - not part of their theme.
I "think" it has to do in the functions.php. I have found some similar questions, and code, but have not had any luck.
I'm a complete noob for .php, so be gentle :)
I've found some other attempts, no luck. Not registering or effecting the feed.
$categories_to_exclude [ 100 ];
$first_cat = false;
$categories = get_the_category( $post->ID );
while ( ! empty( $categories ) && false === $first_cat ) {
if ( in_array($categories[0]->cat_ID, $categories_to_exclude) ) {
array_shift($categories);
}
else {
$first_cat = $categories[0]->cat_ID;
}
}
What I could gather from your question is that You want to ignore one category (may be more) in the related post query?
use the following CODE (some explanation is given within the CODE in comments):
// set the category ID (or multiple category IDs)
// you want to ignore in the following array
$cats_to_ignore = array( 2 );
$categories = wp_get_post_categories( get_the_ID() );
$category_in = array_diff( $categories, $cats_to_ignore );
// ignore only if we have any category left after ignoring
if( count( $category_in ) == 0 ) {
$category_in = $categories;
}
$cat_args = array(
'category__in' => $category_in,
'posts_per_page' => 4,
'orderby' => 'date',
'post__not_in' => array( get_the_ID() )
);
$cat_query = new WP_Query( $cat_args );
while ( $cat_query->have_posts() ) : $cat_query->the_post();
/* just example markup for related posts */
echo '<h2>' . get_the_title() . '</h2>';
endwhile;
// reset $post after custom loop ends (if you need the main loop after this point)
wp_reset_postdata();
Use the below code, it must work
$categories_to_exclude [ 81, 11, 21 ];
$first_cat = false;
$categories = get_the_category( $post->ID );
while ( ! empty( $categories ) && false === $first_cat ) {
if ( in_array($categories[0]->cat_ID, $categories_to_exclude) ) {
array_shift($categories);
}
else {
$first_cat = $categories[0]->cat_ID;
}
}
You get the categories with get_the_category. Then in the while loop you skip the first category if it's 81, and look again. If it's not 81 (and you still have categories available), you asign it to $first_cat and carry on.

Loop multiple post types and posts foreach of them

What i want to make:
I want to make a navigation menu where every dropdownbar is its own post type.
Movie
movie1
movie2
Book
book1
book2
Game
game1
game2
What i've made so far:
I'm not the best at php yet, but i tried to work something out:
echo "<ul class="menu">";
$post_type = get_post_types( array('Movie', 'Book', 'Game') );
foreach( $post_type as $type ) {
$args = array(
'post_type' => $type
);
echo "<li>".$type."<ul class="dropdown">";
$posts = get_posts( $args );
if( $posts ) {
foreach( $posts as $post ) {
echo "<li>".get_the_title( $post->ID, 'title' )."</li>";
}
echo "</ul></li>";
}
}
echo "</ul>";
Question:
Is there a smarter way to make the dropdownmenu? or what can i do to make it work?
There's nothing wrong with that approach, except that you shouldn't use get_post_types() - just an array of the post type names will do.
As it stands at the moment, 'post_type' => $type will pass an array to post_type, when it should be a string.
Also, echo "<li>".$type."<ul class="dropdown">"; should be inside your if( $posts ) { before the foreach.

WordPress: function trashs post type with a new copy of the post

I'm using the following function to trash a custom post type for events.
After an event took place, I delete the trash manually.
I found out, that the posts are still published after they should be deleted. The Version in the trash seems to be a new copy (with a new ID) of the original post?
Is there anything in the function which may produce such an error?
Here is my script:
<?php
function get_delete_old_events() {
$past_query = date('Y-m-d', strtotime('-1 day'));
// WP_Query arguments
$args = array(
'fields' => 'ids', // Only get post ID's to improve performance
'post_type' => array( 'event' ), //post type
'posts_per_page' => '-1',//fetch all posts,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'gid_22',
'value' => $past_query,
'compare' => '<='
)
)
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
$headers[] = 'From: Mail <mail#example.com>';
$postid = get_the_ID();
$post = get_post($postid);
$author = get_userdata($post->post_author);
$subject = "subject....: ".$post->post_title."";
$message = "..."
;
wp_mail($author->user_email, $subject, $message, $headers);
wp_trash_post( $id );
// Also tried with this:
// wp_trash_post( $postid );
}
} else {
// no posts found
return false;
}
// Restore original Post Data
wp_reset_postdata();
}
// expired_post_delete hook fires when the Cron is executed
add_action( 'old_event_delete', 'get_delete_old_events' );
// Add function to register event to wp
add_action( 'wp', 'register_daily_events_delete_event');
function register_daily_events_delete_event() {
// Make sure this event hasn't been scheduled
if( !wp_next_scheduled( 'old_event_delete' ) ) {
// Schedule the event
wp_schedule_event( time(), 'hourly', 'old_event_delete' );
}
}
?>
If you click the trash link then by default Wordpress will keep it for 30 days before permanently deleting it.
wp_trash_post moves an item to the trash if the 30 days havent passed and deletes it if they have, so it's working as I would expect it.
If you want to permanently delete a file straight away use wp_delete_post($post_id, true);
I found my mistake... I'm setting the ID of the post as $postid and using $id instead...

Calling & Displaying Attachments from Sub-Pages - WordPress

I'm new to WordPress (duh) and trying something I'm very close to getting.
I have several Sub-Pages, each of which has attachments - images. I simply want to display these images. The client will regularly be adding more subpages so the code has to know to only display the images of its self, so to speak. An issue I'm running into is each subpage is displaying all the attachments of all subpages.
In my Functions.php I have this code to determine if the page is infact a subpage:
//find out if it is a suppage to use in page.php as function 'is_subpage'
function is_subpage() {
global $post; // load details about this page
if ( is_page() && $post->post_parent ) { // test to see if the page has a parent
return $post->post_parent; // return the ID of the parent post
} else { // there is no parent so ...
return false; // ... the answer to the question is false
}
}
In my page.php I have an If statement, for all my usual pages. It culminates in this:
elseif ( is_subpage() ) {
get_template_part( 'exhibition-template' );
get_template_part( 'normalfooter' );
}
In exhibition-template.php, where I want my images to appear I have this:
<?php
if ( is_subpage( $post->ID ) ) { //I think I screwed up here, innermost brackets
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID, //or is my issue here...?
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => -1,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_link($attachment->ID, 'large', false, false);
}
}
}
?>
However, what happens is each and every subpage simply displays all the images across all subpages, and frankly, I'm just too DUMB to know how to progress, or what to play with, where to start.
Any ideas?
It looks like you have a few issues.
When testing to see if a page is a subpage you should test to see if $post->post_parent is greater than 0. So like this:
if ( is_page() && $post->post_parent > 0 ) {
return $post->post_parent;
} else {
return false;
}
You do not need to pass any arguments into if ( is_subpage( $post->ID ) )... You could also just use WordPress' get_attached_media() function to get these attachements as well. This is how your exhibition-template.php could look:
if ( is_subpage() ) {
$attachments = get_attached_media('image', $post->ID);
foreach ($attachments as $attachment) {
echo wp_get_attachment_link($attachment->ID, 'large', false, false);
}
}

Custom Fields not showing in custom post type post

I have a custom post type named "Designer" Each posts will be using different unique Advanced Custom Fields as each posts has unique templates.With the below code I am able to give rules for each posts in Designer post type and save but the custom fields are not displaying on post edit pages on backend.
Normally this code should ork but no idea what happend to the code
Please Help.
add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types( $choices )
{
$choices['Custom Post types']['cpt_parent'] = 'Custom post type parent';
return $choices;
}
add_filter('acf/location/rule_values/cpt_parent', 'acf_location_rules_values_cpt_parent');
function acf_location_rules_values_cpt_parent( $choices )
{
$args = array(
'hierarchical' => true,
'_builtin' => false
);
$posttypes = get_post_types( $args );
if( $posttypes )
{
foreach( $posttypes as $posttype ):
if( $posttype != 'acf' ):
$args = array(
'post_type' => 'designer',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$customposts = get_posts( $args );
if ( $customposts ) {
foreach( $customposts as $custompost ){
$choices[ $custompost->ID] = $custompost->post_title;
}
}
endif;
endforeach;
}
return $choices;
}
//MATCH THE RULE
add_filter('acf/location/rule_match/cpt_parent', 'acf_location_rules_match_cpt_parent', 10, 3);
function acf_location_rules_match_cpt_parent( $match, $rule, $options )
{
global $post;
$selected_post = (int) $rule['value'];
// post parent
$post_parent = $post->post_parent;
if( $options['page_parent'] ) {
$post_parent = $options['page_parent'];
}
if ($rule['operator'] == "=="){
$match = ( $post_parent == $selected_post );
}
elseif ($rule['operator'] != "!="){
$match = ( $post_parent != $selected_post );
}
return $match;
}
Your Artist Collection field group is set to only appear on one post, the post Designer Post 1 which is a Designer Post type.
I don't understand what all the code is for? Just create a different field group for each post that needs a different field group and a separate rule for each.
Ok sorry I understand the issue now and I have recreated the issue on my local install.
On the line of code below you are looking for the post_parent but I think you should be looking for the ID.
I changed this:
$post_parent = $post->post_parent;
to this:
$post_parent = $post->ID;
and it's working for me.
If I understand your problem correctly, in wp-admin post edit page click on screen options on the upper right corner. In the menu that appears make sure the Custom fields is selected. This will make the custom fields appear for edit.

Categories