Using Customizer to define category_name in Wordpress - wordpress

I am trying to use the Wordpress Customizer to allow the client to input the category names for post that will appear on the homepage. The design calls for two columns of posts, with definable categories for each column.
My hope is that I could do something like:
if( get_theme_mod( 'column_1_category') != "" );
$args = array(
'category_name' => echo get_theme_mod( 'column_1_category'); ),
'posts_per_page' => 2
);
I have already defined the column_1_category in customizer.php, and it works great by itself, but I would like whatever category is typed into the customizer to define the category_name in the $args = array( code.
The 'category_name' = > echo . . is the line that keeps giving me errors. I am assume, if this even works, that I am missing some code in there?
I was hoping this would be a quick and easy way to define the Category, but not sure if this would even work?
Thanks.

Nevermind, i figured it out. All I needed to do was:
$category_2_name = get_theme_mod( 'column_2_content');
$args = array(
'category_name' => ($category_2_name),
'posts_per_page' => 2
);

Related

Sticky posts not showing when using category filter

I am trying to show a specific category post on my home page and in which 1 post is sticky that will appear first but its not working.
I have noticed when I try to show all posts, then sticky post shows first. When I try to show a specific category then its not showing first.
Here is my code:
$sticky = get_option( 'sticky_posts' );
$args = array( 'posts_per_page' => intval($blogtoShow),
'post_status'=>'publish',
'post_type'=>'post',
'cat' => $cattoShow,
'orderby'=>'date',
'post__in' => $sticky);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) :
while( $the_query->have_posts() ) : $the_query->the_post();```
You're looking for the following:
ignore_sticky_posts (boolean) – ignore post stickiness (available since version 3.1, replaced caller_get_posts parameter). false (default): move sticky posts to the start of the set. true: do not move sticky posts to the start of the set.
If you're using a pre-made theme the default might have been modified.
You should add this to your arguments array: 'ignore_sticky_posts' => 0.
A comma should separate each arguments. (Not tested but should work)
More info on the worpress query: https://developer.wordpress.org/reference/classes/wp_query/
----------
EDIT 1.1: I think you need to display a specific template for the sticky (as it's not considered a normal post). At the begining of your loop can you try the following?
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 3,
'post__in' => $sticky,
);
$query = new WP_Query( $args );
if ( $sticky[0] ) {
// insert sticky template...
} else {
// insert posts template...
}

WP Query Get Posts by Comment Author ID and specific keyword?

I am trying to create a custom loop which gets the posts that author__in 1 has commented the specific keyword 'test' on and use that to build the recent posts list.
<?php $query_args = array('search' => 'test', 'author__in' => '1', 'post_status' => 'publish', ); $the_query = new WP_Comment_Query( $query_args );?> <?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
This query doesn't work, it gives me nothing, i guess ->have_posts does not exist for wp_comment_query?
How can i build a query using WP_query that lists the most recent posts with comment 'test' from author with id 1? Is it even possible?
Couple things here.
First, the "search" parameter is actually only "s", not the full word. Here's that reference: https://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
Second, the author__in param takes an array, like this: $query = new WP_Query( array( 'author__in' => array( 2, 6 ) ) ); rather than a single integer or quoted digit. Here's that reference: https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
I think you could get what you want by just changing your code slightly. One thing to note, I'm not sure if the search parameter does a keyword search in comments though, it might only search post content. Try this:
$query_args = array('s' => 'test', 'author__in' => array(1), 'post_status' => 'publish', );
Let me know if this works for you.

How to have the title of the last post in a static page?

I have a page for posts and a different static home page, as well as other common pages, eg About, Sponsors.
I am trying to have in the header of all pages the name of the site and after the title of the last post. I have tried different functions (the_title(), get_the_title()). It works well in the post page, but for all the remaining I get the title of the page ("About" or "Home" or "Sponsors").
I get that the functions I used so far might only work inside the loop, but was wondering if there's some global way to have the title of the last post available everywhere.
I got to: $posts_page = get_option( 'page_for_posts' ) to get the id of the post page and was trying to find a way from there to get the title of the last post, but no success so far.
Is there a simple way to do this?
Try it:
echo current( wp_get_recent_posts( array('numberposts' => 1, 'post_status' => 'publish') ) )['post_title'];
Do you mean the latest/most recent post?
Would this work for you?
<?php
$args = array(
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
);
$post = get_posts( $args );
$latest_post_title = $post[0]->post_title;
echo $latest_post_title;
?>
Or maybe this is a bit more light weight, not sure but they both do pretty much the same thing.
<?php
$args = array(
'numberposts' => 1,
);
$recent = wp_get_recent_posts($args);
$last_post_title = $recent[0]['post_title'];
echo $last_post_title;
?>

Wordpress: retrieve first five items from all populated categories

I am trying to find a way to achive the following: I'd like to make a custom template. Within this template I'd like to list the name of each category on my site that has at least 1 item assigned to it. Beneath the category name (and link) I'd like to insert some content from the first x number of items that have that particular category assigned.
Just in case it makes a massive difference the items in question are not posts, but custom items.
Can anyone give me some pointers or help? I assume there will be something I can do with the wp_query function, but I'm not really sure how to inject it between each of the category titles, or indeed how to make it work with a category for which I can't explicitly provide an id in the code).
Thank you.
get_posts() is your ideal solution. For example:
<?php $posts_array = get_posts( $args ); ?>
<?php $args = array(
'posts_per_page' => 1,
'category' => $post_cat,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'yourcustompostname',
'post_status' => 'publish',
'suppress_filters' => true );
$myposts = get_posts( $args );
///respective loop here ie : foreach $myposts as $mypost ... etc
?>
Where $post_cat is the id of your category and post_type is the custom post type.
To keep these 'items' organized I would create a custom post and then assign a category to those posts, for each respective category (if there are multiple). If there is just one, than you could live with just using a single category.
EDIT:
to get the categories assigned to this custom post take a look at this link
Using the results from this you could assign the cat id's to the variable created earlier: $post_cat.
You have to use query_posts function
query_posts(array('category_name'=>'category-slug','posts_per_page'=>'5'));
then you need to do
while ( have_posts() ) : the_post();
and you can get the post id using:
$p_id = get_the_ID();
for more information : query_posts
You can use get_categories()
http://codex.wordpress.org/Function_Reference/get_categories
But, then you need to find out where the data for 'items' is stored, and query depending on that.
Here is the answer of your problem. You need to change the category id of own post category id .
Here is the code. just copy and past it.
$query1 = new WP_Query( array( 'post_type' => 'post','cat' =>'10,9') );
Use the above query in loop and after loop,reset your query . see the below code.
wp_reset_query();
Please use it and let me know if needs anything else.

wordpress - excluding a category from a post list in a custom theme that doesn't look like the codex

I'm hoping someone can help. I'm not a php coder, but I've been tweeking and customising a premium theme for wordpress anyway, and I'm stuck.
I'm trying to exclude a specific category from a page which lists all the categories by default. Ok, no problem. It should be:
<?php query_posts($query_string . '&cat=-134'); ?>
right?
I'm pretty sure the category number is 134, but I could be wrong. The premium theme I'm using is called Risen, and there's a lot of different kinds of posts - so maybe what I think is a category is really a tag in a custom taxonomy - in which case ???
When I hover over it in the category listing I get this:
example.com/wp-admin/edit-tags.php?action=edit&taxonomy=risen_multimedia_category&tag_ID=134&post_type=risen_multimedia
I'm pretty sure I've found where I need to include my argument, and that is here in the template:
// Get posts
$multimedia_query = new WP_Query( array(
'post_type' => 'risen_multimedia',
'posts_per_page' => risen_option( 'multimedia_per_page' ) ? risen_option( 'multimedia_per_page' ) : risen_option_default( 'multimedia_per_page' ),
'paged' => risen_page_num() // returns/corrects $paged so pagination works on static front page
) );
I've tried adding
'tag' => -134
to this array to no avail.
Being a premium, and apperently tweaked, theme there is a lot of guessing here but I think you have talked yourself into the solution, except for one detail. Use tag__not_in not tag=-134
// Get posts
$multimedia_query = new WP_Query( array(
'post_type' => 'risen_multimedia',
'posts_per_page' => risen_option( 'multimedia_per_page' ) ? risen_option( 'multimedia_per_page' ) : risen_option_default( 'multimedia_per_page' ),
'paged' => risen_page_num() // returns/corrects $paged so pagination works on static front page
'tag__not_in' => array(134)
) );
tag_id=-134 might work (I'd have to test it) but tag expects the tag slug not an ID.
tag (string) - use tag slug
http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters

Resources