i have problem to ad tiitle to title :P
I just have created custom post type. Now i want Get random title from element from custom post type and write it after POST (standard post) here is my code:
function pobierajslowa() {
$args=array(
'orderby' => rand,
'post_type' => 'slowa_kluczowe_wpisu',
'posts_per_page' => 1,
'caller_get_posts'=> 1
);
$queryObject = new WP_Query($args);
while ($queryObject->have_posts()) {
$queryObject->the_post();
return get_the_title();
}
}
function theme_slug_filter_the_content( $title ) {
$custom_content = $title;
$custom_content .= ' '.pobierajslowa().'';
return $custom_content;
}
add_filter( 'the_title', 'theme_slug_filter_the_content' );
After when i use this, i get 503 error :O What i have do wrong?
'orderby' => rand,
Should be...
'orderby' => 'rand', // a string
Make sure to double check syntax. Turning on WP_Debug should tell you where the issues are.
Related
I have a PHP script here I can't seem to wrap my head around, im basically trying to create a shortcode that displays the next custom post type url, then when the current post is also the last post, it loops back pulling the url to the first post.
everything works except for when you are on the final post, the shortcode displays the ID of the current post (final post) instead of the url of the first post, I am assuming it has something to do with " 'fields' => 'ids' " but i have no idea as I don't write php:
function next_post_url() {
$next_post = get_next_post();
if(!$next_post){
$args = array(
'numberposts' => 1,
'post_type' => 'portfolio',
'order' => 'ASC',
'fields' => 'ids'
);
$first_post = get_posts( $args );
$next_post_url = $first_post[0];
}
else{
$next_post_url = get_next_post()->post_title;
}
return get_permalink($next_post->ID);}add_shortcode( 'next_post_url', 'next_post_url' );
Here you go please modify your php code as shown below.
function next_post_url() {
$next_post = get_next_post();
if (!$next_post) {
$args = array(
'numberposts' => 1,
'post_type' => 'portfolio',
'order' => 'ASC',
'fields' => 'ids'
);
$first_post = get_posts($args);
$post = $first_post[0];
} else {
$post = $next_post;
}
return get_permalink($post);
}
add_shortcode('next_post_url', 'next_post_url');
I'm working on a short code for loop through parents ID and display all child page, but I'm not quite sure how to make the loop and make it more custom.
Here's my code:
add_shortcode( 'home-page-listing', 'get_list' );
function get_list( $atts ) {
ob_start();
$atts = shortcode_atts( array(
'ids' => ''
), $atts );
if($atts['ids']!='')
{
$id_array = explode(',',$atts['ids']);
$homePages = new WP_Query( array(
'post_type' => 'page',
'post__in'=>$id_array,
'order' => 'ASC',
'orderby' => 'post__in',
'posts_per_page' => -1
) );
if ($homePages->have_posts()){?>
<div class="">
<?php while ( $homePages->have_posts() ) : $homePages->the_post(); ?>
//here's html template code
<?php endwhile;
wp_reset_postdata(); ?>
</div>
}
}
}
Right now I can use [home-page-listing id=1,2,3,4] to display all select page ID, but I would like to make like this:
[home-page-listing parentID=4]
loop through all child page and display to the font, instead go check all the page id to display.
Thanks!
it's simple used post_parent Arguments of WP_Query. Please check below example
$homePages = new WP_Query( array(
'post_type' => 'page',
'post_parent'=>$parentID,
'order' => 'ASC',
'orderby' => 'parent',
'posts_per_page' => -1
) );
For more information of WP_Query click here
Hi I have created my own custom post type within Wordpress to contain projects that i can call via my theme files.
I am new to creating my own themes. I currently am using the following code in my single.php file to call in related articles based on the category of the blog post.
<?php
// Default arguments
$args = array(
'posts_per_page' => 3, // How many items to display
'post__not_in' => array( get_the_ID() ), // Exclude current post
'no_found_rows' => true, // We don't ned pagination so this speeds up the query
);
// Check for current post category and add tax_query to the query arguments
$cats = wp_get_post_terms( get_the_ID(), 'category' );
$cats_ids = array();
foreach( $cats as $wpex_related_cat ) {
$cats_ids[] = $wpex_related_cat->term_id;
}
if ( ! empty( $cats_ids ) ) {
$args['category__in'] = $cats_ids;
}
// Query posts
$wpex_query = new wp_query( $args );
// Loop through posts
foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<div class="col-md-4 related-post">
<?php the_post_thumbnail('large'); ?>
<?php the_title(); ?>
</div>
<?php
// End loop
endforeach;
// Reset post data
wp_reset_postdata(); ?>
In my new post type "projects" i would like to call in related projects. Which im assuming would be very similar code except i need to stop it looking for posts and instead look for my projects.
Here is my code for new post type:
// Projects
add_action( 'init', 'create_post_type' );
add_post_type_support( 'bw_projects', 'thumbnail' );
add_post_type_support( 'bw_projects', 'custom-fields' );
function create_post_type() {
register_post_type( 'bw_projects',
array(
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Projects' )
),
'public' => true,
'has_archive' => true,
'taxonomies' => array( 'category','post_tag'),
)
);
}
What would i need to change in my first code snippet in order to look for bw_projects and not look for 'posts' anymore. I tried playing around and changing certain lines myself but i caused more issues and stopped the page loading. Is this even right i can use the same code, slightly altered or would i need something completely different?
Thanks in advance.
You can get any post type that you require using get_posts();
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'projects',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Simply set the 'post_type' argument to that of you custom post type, to get only these posts. You can also set the number of post, and filter by category etc.
You can find more info in the codex.
Alternatively, if you wanted to keep something similar to your existing code you could try using 'pre_get_posts' to filter the query to just your projects. However you'd need to remember to add / remove this filter so it only operates on the queries that need it.
To display the posts you can use a simple foreach to churn them out. You#d obviously want to do some sort of styling to get the layout correct:
$args = array("posts_per_page" => 10, "orderby" => "comment_count");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
Or a really concise way of doing all of the above would be something like:
$args = array("posts_per_page" => 5, "post_type" => "projects");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
I am new on wordpress and try to make a widget which will only show the last post of particuler category.
TO do this I have given some settings in admin to select category for which admin wants to display last post (post of last ID).
I am using the below code to in widget to display post content:
$query_arguments = array(
'posts_per_page' => (int) 1,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => (int) $settings['category'],
'order' => 'DESC',
'orderby' => (($settings['display_by'] == 0) ? 'ID' : 'date')
);
$posts_query = new WP_Query( $query_arguments );
if ($posts_query->have_posts()) {
$posts = $posts_query->get_posts();
$post = $posts[0];
echo $post->post_content;
}
But the above code is showing content in one paragraph or you can say that without format. I have done lot of search and found that, I need to apply "the_content" filter to format the content. So I have done the same as below code:
if ($posts_query->have_posts()) {
$posts = $posts_query->get_posts();
$post = $posts[0];
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Now the above changes is returning the null string. I have google lot of things but everyone is saying that to use apply filter or use the_content() function. I have tried both solutions but nothing happening.
Can anyone please share the solution for this problem?
please check whether you are getting any text in $post->post_content
by echo $post->post_content
All you need to do is,
$query_arguments = array(
'posts_per_page' => (int) 1,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => (int) $settings['category'],
'order' => 'DESC',
'orderby' => (($settings['display_by'] == 0) ? 'ID' : 'date')
);
$posts_query = new WP_Query( $query_arguments );
while ($posts_query->have_posts()) {
$posts_query->the_post();
echo get_the_content();
}
I'm writing a plugin and I need to show admin notices about the posts. I called to the function which provides messages.
add_action('admin_notices', array($this, 'postNotices'));
And here is my function :
public function postNotices() {
$args = array(
'post_type' => 'demo_post_type',
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => 1
);
$query_post = new WP_Query($args);
if ($query_post->have_posts()) {
while ($query_post->have_posts()) {
$query_post->the_post();
// $my_meta = 'Im getting post meta here'; ?>
<div>Some notification</div>
<?php wp_reset_query();
}
}
}
Notices showing is working correctly but When I try to create a new post it appears last post data such as post title in the new post page.
It means wp_reset_query() function is not working with admin_notices hook I think. Has anyone a suggestion?
Any suggestions are highly appreciated.
After hard research I foud the answer,
Here we go,
$args = array(
'post_type' => 'demo_post_type',
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => 1
);
$query_posts = get_posts($args);
foreach ($query_posts as $post) {
?>
<div>Some notification</div>
<?php
}
No need to add functions like wp_reset_query(). Found the answer but don't know what the logic is. if anyone know, Please comment here.