WordPress: retroactively add parent category - wordpress

I have a WP installation with Custom Post Types. My CPT has custom taxonomy. When adding posts, we only added child categories to posts and left the parent category unselected.
Is it possible to automatically add the parent category retroactively to the post? The child category should stay the primary category. I don't want to edit all 200+ posts by hand.

You should loop through your posts and apply (append) the new category. Something like this:
$args = array(
'post_type' => 'myposttype'
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
wp_set_post_categories(newcat_ID, true)
endwhile;
}
wp_reset_query();

Related

Woocommerce and Elementor template problem

I have web site WP + Woocommerce + Elementor. Im realise custom filter product by attribute values. But im not get normal template products item cells.
Im using this code, by ajax request:
$args = $this->filter_query_build(); // Create $args for WP_Query
$query = new WP_Query( $args );
$products='';
if ( $query->have_posts() )
{
while ( $query->have_posts() ) : $query->the_post();
ob_start();
wc_get_template_part( 'content', 'product' );
$products.= ob_get_contents(); // Get product item
ob_end_clean();
endwhile;
}
// Create JSON output
echo json_encode([
'products'=>$products,
]);
wp_die();
Result work this code is fine, i have list product.
But the html item product not equal html in category list or archive-product.
Product category and archive-product create by in Elementor.
Im need get template item cells product equal output on the product category and archive-product.
Tried to figure out where the elementor template is pulling from, but so far without success

WP_Query returning unlimited number of posts, showposts and posts_per_page not working

I'm trying to hack a WordPress Theme that uses WP_Query for a "Recent Posts" widget. Currently it is limited to return only a specific Category, while I'd like it to return a limited number of posts from all categories. I have tried and succeeded at this before with other Plugins/Themes. In this case it doesn't work, irrespective of what change I try to implement, I get 4000+ posts in the query.
<?php $the_query = new WP_Query('cat='.$category.'&showposts='.$postnum);
while ($the_query->have_posts()) : $the_query->the_post();?>
What could be the common culprit of not having showposts or post_per_page working correctly as a limiter?
are you able to get a correct output from your variable $postnum ?
also try using this
$args = array('category_name' => 'my-category-slug', 'posts_per_page' => 3);
<?php
query_posts( $args );
while ( have_posts() ) : the_post();
echo 'content';
endwhile;
wp_reset_query();
?>

get post of category in wordpress

I'm Using WP 3.4.2 , a child theme of twentyeleven. I have created a category called "featured media".
I want to get and display posts which have this category.
I checked around before coming here, I found variations of the same answer, in forums. Generally I am advised to write:
$args = array('category'=> x);
get_posts($args);
I don't know the category id. Some forums have advised me to go to Dashboard--> posts -->categories, find my category and hover over the "Edit" link, and read the category id from the status bar. My browser shows me this address: localhost/myblog/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=7&post_type=post
This apparently means my category id is 7. The above code returns and empty array. This has become really frustrating for me. I believe the method is correct. Can anyone tell me what I'm doing wrong?
Are you setting global $post?
Heres the WordPress example from their website:
<ul>
<?php
global $post;
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
http://codex.wordpress.org/Template_Tags/get_posts

Show the_content() from specific category

I need to find out how to list my content data on a page/ section when I click on a specific category link.
After a few hours I realise that help from stackoverflow is what i need!
The function remind a little of: http://www.hashbangcode.com/blog/wordpress-category-post-list-493.html
I need to list my data (title, the_content etc) from the specific category and not all categories with the titles.
While that post gives you a good start, here is the code that I would use
// Set the desired category
$category = 1;
// Make query for posts in the category
$my_query = new WP_Query();
$my_query->query(
array(
'cat' => $category,
// Does not show sticky posts; use 'caller_get_posts' if using < WP 3.1
'ignore_sticky_posts' => 1
)
);
// Make sure some posts were found.
if($my_query->have_posts())
{
// Loop through each post found.
while($my_query->have_posts())
{
// Setup the post data to use
$my_query->the_post();
global $post;
// Echo out the title; Note that no formatting has been done
the_title();
the_content();
}
}
Now, you can also get the title with either:
$title = get_the_title($post->ID);
$title = $post->post_title;
Additionally, you can get the post content with:
$content = $post->post_content;
Also, you can get the category using any of these parameters:
cat (int) - use category id.
category_name (string) - use category slug (NOT name).
category__and (array) - use category id.
category__in (array) - use category id.
category__not_in (array) - use category id.
More about the WP_Query class can be found here: http://codex.wordpress.org/Function_Reference/WP_Query

Wordpress - related posts by custom taxonomy problem

I'm trying to display related posts based on a custum taxonomy. I found a query at wordpress.org that kind of works. However the original post gets duplicated in the results multiple times. (words is the name of the custom taxonomy I use) What seems to happen is that the single post gets duplicated according to what amount showpost is set. Any idea's what could cause this?
The code:
<?php
//for in the loop, display all "content", regardless of post_type,
//that have the same custom taxonomy (e.g. words) terms as the current post
$backup = $post; // backup the current object
$found_none = '<h2>No related posts found!</h2>';
$taxonomy = 'words';// e.g. post_tag, category, custom taxonomy
$param_type = 'words'; // e.g. tag__in, category__in, but genre__in will NOT work
$post_types = get_post_types( array('public' => true), 'names' );
$tax_args=array('orderby' => 'none');
$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
if ($tags) {
foreach ($tags as $tag) {
$args=array(
"$param_type" => $tag->slug,
'post__not_in' => array($post->ID),
'post_type' => $post_types,
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php $found_none = '';
endwhile;
}
}
}
if ($found_none) {
echo $found_none;
}
$post = $backup; // copy it back
wp_reset_query(); // to use the original query again
?>
It's inside the foreach loop that you're getting duplications. That code is effectively saying;
Get all the terms for taxonomy type $param_type
For each term, get 5 posts that are tagged with that term
So if you have a post that is tagged with more than one term of the same taxonomy, it's likely it will appear more than once.
You can iteratively add queried posts into the post__not_in array to ensure they don't appear again;
Add $post_not_in = array($post->ID); just above if ($tags) {
Then replace the line post__not_in' => array($post->ID), with post__not_in' => $post_not_in,.
Finally, drop $post_not_in[] = get_the_ID(); inside your while loop, after $found_none = '';
As for me i use this plugin for custom taxonomy relate post. I hope that plugin will help your problem.

Resources